Compose mail in the iPhone app Compose mail in the iPhone app ios ios

Compose mail in the iPhone app


Here's the code:

Obj-C:

(Don't forget to add the messageUI framework to your project!!!)

First import the message library:

#import <MessageUI/MessageUI.h>

Then mark your self as a delegate like this:

@interface MYViewController () <MFMailComposeViewControllerDelegate>

Then to pull up the composer (if user has email set up on their device):

- (IBAction)emailButtonPressed:(id)sender {    if ([MFMailComposeViewController canSendMail]) {        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];        [composeViewController setMailComposeDelegate:self];        [composeViewController setToRecipients:@[@"example@email.com"]];        [composeViewController setSubject:@"example subject"];        [self presentViewController:composeViewController animated:YES completion:nil];    }}

Then to handle the delegate callback and dismiss the composer:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {    //Add an alert in case of failure    [self dismissViewControllerAnimated:YES completion:nil];}

SWIFT 3:

Import the relevant library:

import MessageUI

Mark your view controller as a delegate like so:

class MyViewController: UIViewController, MFMailComposeViewControllerDelegate {

Pull up composer (if user has email set up on their device):

@IBAction func emailButtonAction(_ sender: UIButton) {                if MFMailComposeViewController.canSendMail() {            let mail = MFMailComposeViewController()            mail.mailComposeDelegate = self            mail.setToRecipients(["example@gmail.com"])            mail.setSubject("Example Subject")            mail.setMessageBody("<p>Test</p>", isHTML: true)            present(mail, animated: true)        }    }

Handle delegate callback and dismiss the composer:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {    controller.dismiss(animated: true)}


You have to link against the MessageUI framework and use the class MFMailComposeViewController. Don't forget to import the framework (#import <MessageUI/MessageUI.h>).

The documentation with sample code: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html


Here is the code to open Mail Composer in iOS:

Source:http://sickprogrammersarea.blogspot.in/2014/03/open-mail-composer-programmatically-in.html

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];      // Do any additional setup after loading the view, typically from a nib.    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];    [button addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchDown];    [button setTitle:@"Send Mail" forState:UIControlStateNormal];    button.frame = CGRectMake(100.0, 350.0, 100.0, 40.0);    [self.view addSubview:button];}- (void) send:(id)sender{    MFMailComposeViewController *comp=[[MFMailComposeViewController alloc]init];    [comp setMailComposeDelegate:self];    if([MFMailComposeViewController canSendMail])    {        [comp setToRecipients:[NSArray arrayWithObjects:@"imstkgp@gnail.com", nil]];        [comp setSubject:@"From my app"];        [comp setMessageBody:@"Hello bro" isHTML:NO];        [comp setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];        [self presentViewController:comp animated:YES completion:nil];    }    else{        UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];        [alrt show];    }}-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{    if(error)    {        UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];        [alrt show];        [self dismissModalViewControllerAnimated:YES];    }    else{        [self dismissModalViewControllerAnimated:YES];    }}