The iOS SDK has made it really easy to send email using the built-in APIs. With a few line of codes, you can launch the same email interface as the stock Mail app that lets you compose an email. In this tutorial, we’ll build a very simple app to show you how to send both plain text and HTML email using the iOS SDK.
Create a Simple View App with a Button
First, create a simple app with a view controller and a button. If you have no idea how to do it, you better revisit the Hello World tutorial. Let’s name the project as SimpleEmail. If you’ve successfully created your project, add a “Contact Us” button in the view.
SimpleEmail App with Contact Us Button
Connecting the Contact Us Button with Action
In the past tutorials, we used to add the action method manually in the header file of the view controller file. We then link the button and the action method in the Interface Builder.This time, let us show you another way to add the action method (make sure you use Xcode 4.3 or up).
In the Project Navigator, select the “SimpleEmailViewController.xib” file to go to the Interface Builder. Switch to the Assistant Editor and hide the Utility area:
Show Assistant Editor and Hide Utility Area
User Interface and Source Code Displayed Side by Side
Prompt to Let You Insert Outlet and Action
Add action method - showEmail
Xcode Automatically Inserts the showEmail Method
Implementing the Email Interface
Next, import the “MessageUI.h” and implement the “MFMailComposeViewControllerDelegate” delegate in SimpleEmailViewController.h.
1
2 3 4 5 6 7 |
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h> @interface SimpleEmailViewController : UIViewController <MFMailComposeViewControllerDelegate> // Add the delegate - (IBAction)showEmail:(id)sender; @end |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
- (IBAction)showEmail:(id)sender {
// Email Subject NSString *emailTitle = @"Test Email"; // Email Content NSString *messageBody = @"iOS programming is so fun!"; // To address NSArray *toRecipents = [NSArray arrayWithObject:@"support@appcoda.com"]; MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; mc.mailComposeDelegate = self; [mc setSubject:emailTitle]; [mc setMessageBody:messageBody isHTML:NO]; [mc setToRecipients:toRecipents]; // Present mail view controller on screen [self presentViewController:mc animated:YES completion:NULL]; } - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { switch (result) { case MFMailComposeResultCancelled: NSLog(@"Mail cancelled"); break; case MFMailComposeResultSaved: NSLog(@"Mail saved"); break; case MFMailComposeResultSent: NSLog(@"Mail sent"); break; case MFMailComposeResultFailed: NSLog(@"Mail sent failure: %@", [error localizedDescription]); break; default: break; } // Close the Mail Interface [self dismissViewControllerAnimated:YES completion:NULL]; } |
Line 13 of the code invokes the presentViewController to display the mail interface on screen.
The “didFinishWithResult:” is a method of the MFMailComposeViewControllerDelegate protocol. This method will be automatically called when the mail interface is closed (e.g. user cancels the operation). The result parameter tells you the result code when the mail composition interface is dismissed. For the sake of simplicity, we simply log the status in this example. In real world application, you should provide special handling for the result code (e.g. display an alert when the app fails to send the email).
Finally, line 41 dismisses the mail composition interface.
Adding the MessageUI Framework
However, this is not done yet. If you try to compile the app, you’ll end up with the below compilation error:
SimpleEmail App Compilation Error
To fix the error, we have to embed the framework in the project. In the Project Navigator, select “SimpleEmail” project and then select the “SimpleEmail” target under “Targets”. Click “Build Phases” at the top of the project editor. Then open the Link Binary With Libraries section.
Add Framework in Build Phases
Add MessageUI Framework in Your Xcode Project
SimpleEmail App
Composing HTML Email
The SimpleEmail app now only supports plain text email. You can easily enable the support of HTML email by changing the “isHTML” parameter in “setMessageBody” method from “N” to “Y”. Try to change the code in “showEmail” method to the following:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
- (IBAction)showEmail:(id)sender {
// Email Subject NSString *emailTitle = @"Test Email"; // Email Content NSString *messageBody = @"<h1>Learning iOS Programming!</h1>"; // Change the message body to HTML // To address NSArray *toRecipents = [NSArray arrayWithObject:@"support@appcoda.com"]; MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; mc.mailComposeDelegate = self; [mc setSubject:emailTitle]; [mc setMessageBody:messageBody isHTML:YES]; [mc setToRecipients:toRecipents]; // Present mail view controller on screen [self presentViewController:mc animated:YES completion:NULL]; } |
SimpleEmail App with HTML Support
No comments:
Post a Comment