In this blog post I am going to elaborate a simple example to Open Mail Composer programmatically in iOS using Objective-C. I would also provide the code from a demo project where I have already integrated this feature for my app. I have used the code to send mails via my own iOS app to various other devices. The code is also tested on iOS8.
Code to open MailComposer programmatically in iOS:

MailViewController.h

#import <UIKit/UIKit.h> #import <MessageUI/MessageUI.h> @interface MailViewController:UIViewController<MFMessageComposeViewControllerDelegate> @end

MailViewController.m

#import "MailViewController.h" @interface MailViewController () @end @implementation MailViewController - (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]; } }
So as you can see in the above code I have created a UIButton on click of which I have created an object of MFMailComposeViewController and then checked that whether my device supports the mail sending facility or not. If the device supports the mail sending facility then I  have set the To, Subject and Body of the mail by using the respective properties of MFMailComposeViewController and set the delegate of the mail composer object to self. Finally I implemented the delegate method of the MFMailComposerViewController which is called when the mail is either successfully send or some error occurred while sending the mail where I have checked for errors and then shown an alert view in case of error.

Hope the above code will be helpful for you. For any issues feel free to contact me at my mail or post your issue as comment below. I will try to respond as soon as possible.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.