~If you would like to request a tutorial, email Collateral at collateraldamag3@gmail.com~

Share This!

iPhone Tutorial ~ In-App Email


Ok, so here is a tutorial for getting the Mail.app modal view in your application for OS 3.0+ and to launch Mail.app from your application if your on 2.X.X.
*Heads up: Clicking on the images will zoom them ;) 
99% of the time, this is going to be run as an event, or a response to user input (pressing a UIButton, responding to a UIAlertView, etc.), so right off the bat, you know its going to be a -(IBAction) or - (void). 
First, however, we are going to want to import the mail framework, titled "MessageUI.framework". So right click on the "Frameworks" folder, then click "Add" then "Existing Frameworks" then "MessageUI.framework". Now you should see this: 

Then you are going to want to add this to your import statements: 

This will allow you to access those oh-so-special frameworks you just added. We now just have to add the methods to call the Mail.app modal view.  So add these to your .h file under your @property calls.

- (void)displayComposerSheet;
- (void)showPicker;
- (void)launchMailAppOnDevice;
- (IBAction)callMail;

 This is the call for the methods that we are going to define in your .m file. Speaking of which, turn to your .m file now, and I will break down what is necessary to call to make this puppy work. 
First, we need an -(IBAction) to call our method. This is what callMail { } is for. So make a UIButton and assign callMail { } to it. If don't know how to do this, then the easiest way in in the Interface Builder. In the IB, click on "File's Owner" and then on the "Connections" tab in the inspector. You should see "Received Actions" and under that, "callMail". Click on the "+" sign next to callMail, and drag it to your UIButton. It will give you a drop-down menu. Choose "Touch Up Inside." As shown: 

Save your .XIB and go back to your .m file. Type: 

- (IBAction) callMail{
[self showPicker];

Ok, now your - (IBAction) is done. Wasn't that easy? Now here comes the method definitions. Just type them in exactly as i write them:

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
[self displayComposerSheet];
[self launchMailAppOnDevice];
[self launchMailAppOnDevice];

This is the method we just called.  This method checks if the device can bring up the modal view. If it can, it calls the method to do so. If it can't, it tells the divice to open Mail.app. Now we need to call the modal view itself:

// Displays an email composition interface inside the application. Populates all the Mail fields.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"SUBJECT HERE"];

// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"bob@john.com"];
//NSArray *ccRecipients = [NSArray arrayWithObjects:@"", nil];
//NSArray *bccRecipients = [NSArray arrayWithObject:@""];

[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];

// Fill out the email body text
NSString *emailBody = (@"BODY TEXT HERE");
[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
    [picker release];

The above will call the mail composition GUI as a modal view inside your application. From here, you can set the subject, recipients, cc's, bcc's and the body text. (This, of course, ends up being totally editable by the end user, however). 

// Dismisses the email composition interface when users tap Cancel or Send.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
[self dismissModalViewControllerAnimated:YES];

This, as the comment says, dismissed the composition view when the user sends or cancels the email. 
And finally we must type the method  to allow the 2.X.X users to call their Mail.app. 

// Launches the Mail application on the device.
NSString *recipients = @"bob@john.com";
NSString *body = (@"BODY TEXT HERE");

NSString *email = [NSString stringWithFormat:@"%@%@",recipients,body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
And there you have it. When you click your UIButton, In-app mail should now work :)

Code based on Apple's examples. **

1 comment:

Anonymous said...

the article seemed (at least to me) to explain how to use the apple-mailapp inside an app. But it only explains how to use the "write-mail-from-an-app" function.

that part works but I feel kind aof missleaded anyway...

Post a Comment

This entry is filed under .

You can also follow any responses to all entry through the RSS Comments feed.