Tuesday, January 24, 2012

Create In-App email sent from iPhone, iPad

Before write the code, set up the followings in the project.

Add the Framework
Add the MessageUI framework to your projects Frameworks group folder in Xcode

Import Message Framework
Import MessageUI.h and MFMailComposeViewController.h to the header file of the delegate
Make your Delegate
Indicate that your delegate class is the delegate by including this <MFMailComposeViewControllerDelegate> in the 
.h file
Implement the Delegate Method
Implement the didFinishWithResult MFMailComposeViewControllerDelegate delegate method and make sure to return control to the program by sending the dismissModalViewControllerAnimated message.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"mySubject"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"myRecipient@abc.com"]; 
[picker setToRecipients:toRecipients];
// Fill out the email body text
NSString *emailBody = @"message body.";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
        [picker release];

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 

         [self dismissModalViewControllerAnimated:YES];



        NSString *str = [NSString stringWithFormat:@"mailto:%@&subject=mySubject",recipient];
NSString *body = @"&body=message content";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

No comments:

Post a Comment