Posted By

yasirmturk on 09/18/11


Tagged

iphone iPad MFMailComposeViewController


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

yasirmturk
cnathan


Display Email Composer on iPhone SDK


 / Published in: Objective C
 

URL: http://yasirmturk.com

Add MessageUI.framework reference

then in header file

import <MessageUI/MessageUI.h> import <MessageUI/MFMailComposeViewController.h> import <MessageUI/MFMessageComposeViewController.h>
  1. // Displays an email composition interface inside the application. Populates all the Mail fields.
  2. - (void) displayComposerSheet:(NSString *)body {
  3.  
  4. MFMailComposeViewController *tempMailCompose = [[MFMailComposeViewController alloc] init];
  5.  
  6. tempMailCompose.mailComposeDelegate = self;
  7.  
  8. //[tempMailCompose setToRecipients:[NSArray arrayWithObject:@"[email protected]"]];
  9. //[tempMailCompose setCcRecipients:[NSArray arrayWithObject:@"[email protected]"]];
  10. [tempMailCompose setSubject:@"iPhone App recommendation"];
  11. [tempMailCompose setMessageBody:body isHTML:NO];
  12.  
  13. [self presentModalViewController:tempMailCompose animated:YES];
  14. [tempMailCompose release];
  15. }
  16.  
  17. // Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
  18. - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
  19. // Notifies users about errors associated with the interface
  20. switch (result)
  21. {
  22. case MFMailComposeResultCancelled:
  23. NSLog(@"Result: canceled");
  24. break;
  25. case MFMailComposeResultSaved:
  26. NSLog(@"Result: saved");
  27. break;
  28. case MFMailComposeResultSent:
  29. NSLog(@"Result: sent");
  30. break;
  31. case MFMailComposeResultFailed:
  32. NSLog(@"Result: failed");
  33. break;
  34. default:
  35. NSLog(@"Result: not sent");
  36. break;
  37. }
  38. [self dismissModalViewControllerAnimated:YES];
  39. }
  40.  
  41. // Launches the Mail application on the device. Workaround
  42. -(void)launchMailAppOnDevice:(NSString *)body{
  43. NSString *recipients = [NSString stringWithFormat:@"mailto:%@?subject=%@", @"[email protected]", @"iPhone App recommendation"];
  44. NSString *mailBody = [NSString stringWithFormat:@"&body=%@", body];
  45.  
  46. NSString *email = [NSString stringWithFormat:@"%@%@", recipients, mailBody];
  47. email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  48.  
  49. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
  50. }
  51.  
  52. // Call this method and pass parameters
  53. -(void) showComposer:(id)sender{
  54. Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
  55. if (mailClass != nil){
  56. // We must always check whether the current device is configured for sending emails
  57. if ([mailClass canSendMail]){
  58. [self displayComposerSheet:sender];
  59. }else{
  60. [self launchMailAppOnDevice:sender];
  61. }
  62. }else{
  63. [self launchMailAppOnDevice:sender];
  64. }
  65. }

Report this snippet  

You need to login to post a comment.