oscarimonbox
6/2/2016 - 11:05 AM

enviar-mail

enviar-mail

let email = "foo@bar.com"
if let url = URL(string: "mailto:\(email)") {
  if #available(iOS 10.0, *) {
    UIApplication.shared.open(url)
  } else {
    UIApplication.shared.openURL(url)
  }    
}
1) Add MessageUI framework:
Click on the project
Select "Build Phases"
Expand "Link Binary With Libraries"
Click "+" and type "Message" to find "MessageUI" framework, then add.


2) In current view controller add import and implement a protocol:
#import <MessageUI/MessageUI.h> 
#import <MessageUI/MFMailComposeViewController.h> 
@interface MyViewController : UIViewController<MFMailComposeViewControllerDelegate>


3) Add methods:

    -(void)sendEmail {
        // From within your active view controller
        if([MFMailComposeViewController canSendMail]) {
            MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
            mailCont.mailComposeDelegate = self;        // Required to invoke mailComposeController when send

            [mailCont setSubject:@"Email subject"];
            [mailCont setToRecipients:[NSArray arrayWithObject:@"myFriends@email.com"]];
            [mailCont setMessageBody:@"Email message" isHTML:NO];

            [self presentViewController:mailCont animated:YES completion:nil];
        }
    }

    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
        [controller dismissViewControllerAnimated:YES completion:nil];
    }