Return to Snippet

Revision: 51220
at September 18, 2011 00:29 by yasirmturk

Initial Code
// Displays an email composition interface inside the application. Populates all the Mail fields. 
- (void) displayComposerSheet:(NSString *)body {
	MFMailComposeViewController *tempMailCompose = [[MFMailComposeViewController alloc] init];
	tempMailCompose.mailComposeDelegate = self;
	//[tempMailCompose setToRecipients:[NSArray arrayWithObject:@"[email protected]"]];
	//[tempMailCompose setCcRecipients:[NSArray arrayWithObject:@"[email protected]"]];
	[tempMailCompose setSubject:@"iPhone App recommendation"];
	[tempMailCompose setMessageBody:body isHTML:NO];
	[self presentModalViewController:tempMailCompose animated:YES];
    [tempMailCompose release];

// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {	
	// Notifies users about errors associated with the interface
	switch (result)
		case MFMailComposeResultCancelled:
			NSLog(@"Result: canceled");
		case MFMailComposeResultSaved:
			NSLog(@"Result: saved");
		case MFMailComposeResultSent:
			NSLog(@"Result: sent");
		case MFMailComposeResultFailed:
			NSLog(@"Result: failed");
			NSLog(@"Result: not sent");
	[self dismissModalViewControllerAnimated:YES];

// Launches the Mail application on the device. Workaround
-(void)launchMailAppOnDevice:(NSString *)body{
	NSString *recipients = [NSString stringWithFormat:@"mailto:%@?subject=%@", @"[email protected]", @"iPhone App recommendation"];
	NSString *mailBody = [NSString stringWithFormat:@"&body=%@", body];
	NSString *email = [NSString stringWithFormat:@"%@%@", recipients, mailBody];
	email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

// Call this method and pass parameters
-(void) showComposer:(id)sender{
	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:sender];
			[self launchMailAppOnDevice:sender];
		[self launchMailAppOnDevice:sender];

Initial URL

Initial Description
Add MessageUI.framework reference

then in header file

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MFMessageComposeViewController.h>

Initial Title
Display Email Composer on iPhone SDK

Initial Tags

Initial Language
Objective C