This post will show you how you can include in-apps email features in your iOS application. Apple has provided its developers with a framework called MessageUI.framework using which developers can include in-apps email features in their iOS applications. This post will show how you can use this framework to send emails from your apps.
Lets us create a Single View Application project in Xcode and name it "InAppEmail" or you can use different name as you please.
Now add MessageUI.framework to your project.
Skip this paragraph if you know how to add the framework. Go to Build Phases=>Link Binary With Libraries.=>Click on the "+" sign at the bottom of the section=>Look for or type MessageUI=> Select it and click Add button.
Now in your header InAppEmail.h file
->import these header files
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
->include the delegate
<MFMailComposeViewControllerDelegate>
->declare these two methods
-(void)mail_clicked;
-(void)displayComposePage;
-(void)displayComposePage;
Now in your implementation InAppEmail.m file
->include these code in your viewDidLoad method
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(110, 200, 100, 50);
[button setTitle:@"Send Mail" forState:UIControlStateNormal];
[button addTarget:self action:@selector(mail_clicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Here. we created a button which is set to call the mail_clicked method when clicked and added as a subview of the present viewController.
->body of the methods
-(void)mail_clicked{
Class emailClass=(NSClassFromString(@"MFMailComposeViewController"));
if (emailClass!=nil)
{
if ([emailClass canSendMail])
[self displayComposePage];
else
{
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Email cannot be send!"
message:@"Device is not configured to send email"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
else
{
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Email cannot be send!"
message:@"Device cannot send email"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
Here, we first check if the class is correctly loaded and if TRUE we again check if this class can send mail which if TRUE will called the method displayComposePage
Note: NSClassFromString(classNameString) returns the class object named by classNameString, or nil if no class by that name is currently loaded. [emailClass canSendMail] will return TRUE if the device is configured to send email else NO.
-(void)displayComposePage{
MFMailComposeViewController *composePage=[[MFMailComposeViewController alloc]init];
composePage.mailComposeDelegate=self;
NSArray *recipients=[NSArray arrayWithObject:@"aj@google.com"];
[composePage setToRecipients:recipients];
[composePage setSubject:@"This is a test email."];
[composePage setMessageBody:@"Hi, I just mailed to say I love you." isHTML:NO];
[composePage setTitle:@"Email"];
composePage.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:composePage animated:YES completion:nil];
}
Here, we create a MFMailComposeViewController class object called composePage and we set the delegate to self. The following lines of codes except the last one are not necessary. These are used to set the different fields like recipients, subject, body etc. in the mail. You can directly jump to the last line of codes and fill all the necessary fields in the compose page as well.
Here, we create a MFMailComposeViewController class object called composePage and we set the delegate to self. The following lines of codes except the last one are not necessary. These are used to set the different fields like recipients, subject, body etc. in the mail. You can directly jump to the last line of codes and fill all the necessary fields in the compose page as well.
Finally, we need to include the delegate method for Mail Compose Controller
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
[self dismissViewControllerAnimated:YES completion:nil];
}
This delegate method is called when you finally click send or cancel button. Here, we simply set the method to dismiss the Mail Compose Controller.
Suggestions and corrections are most welcome. If you have any, please do so without hesitation.
Done!
Now compile and run your code. So if the above code works all fine you should see the compose mail page when you click the button titled Send Mail. FYI, the simulator doesn't actually send mail, it simply behaves as if the mail has been sent. You have to run the app in the actual device to send the mail in real.
Suggestions and corrections are most welcome. If you have any, please do so without hesitation.
thank you so much :) you helped alot
ReplyDeleteThank you Sooo much !!! You saved alot of time...
ReplyDeleteSuperb explanation & it's too clear to understand the concept as well, keep sharing admin with some updated information with right examples.Keep update more posts.
ReplyDeleteBack to Original Services Private Limited