Hi! I'm looking for a mfmailcomposeviewcontroller example for both iPhone and iPad. Any suggestion??
Thanks!!
To configure a mail in your iPhone / iPad application, import the given class in your header file. And add a new framework called "MessageUI.framework" to your frameworks folder.
#import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h>
We also need to create a label to show the message and button action to perform actions as showPicker, displayComposerSheet, launchMailAppOnDevice as given below..
-(IBAction)showPicker:(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]; } else { [self launchMailAppOnDevice]; } } else { [self launchMailAppOnDevice]; } } -(void)displayComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Hello from California!"]; // Set up recipients NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; [picker setToRecipients:toRecipients]; [picker setCcRecipients:ccRecipients]; [picker setBccRecipients:bccRecipients]; // Attach an image to the email NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"]; // Fill out the email body text NSString *emailBody = @"It is raining in sunny California!"; [picker setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:picker animated:YES]; [picker 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 { message.hidden = NO; // Notifies users about errors associated with the interface switch (result) { case MFMailComposeResultCancelled: message.text = @"Result: canceled"; break; case MFMailComposeResultSaved: message.text = @"Result: saved"; break; case MFMailComposeResultSent: message.text = @"Result: sent"; break; case MFMailComposeResultFailed: message.text = @"Result: failed"; break; default: message.text = @"Result: not sent"; break; } [self dismissModalViewControllerAnimated:YES]; } // Launches the Mail application on the device. -(void)launchMailAppOnDevice { NSString *recipients = @"mailto:[email protected][email protected],[email protected]&subject=Hello from California!"; NSString *body = @"&body=It is raining in sunny California!"; NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]]; }
You can find a running example in Apple website for developers too :-)
Ads