在iPhone SDK上显示电子邮件生成器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在iPhone SDK上显示电子邮件生成器相关的知识,希望对你有一定的参考价值。

Add MessageUI.framework reference

then in header file

#import
#import
#import
  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. }

以上是关于在iPhone SDK上显示电子邮件生成器的主要内容,如果未能解决你的问题,请参考以下文章

您可以使用 iOS6 SDK 构建但在 iPhone 5 上保持信箱吗?

从用户获取输入文本并使用 iPhone SDK 将其保存在网络服务器上

Iphone SDK:在 iphone 应用程序的后台发送电子邮件

如何捕获当前视图屏幕截图并在代码中重用? (iPhone SDK)

iPhone SDK 3.0 应用内电子邮件 - 更改导航栏色调颜色

在 iPhone SDK 上显示类似静音/音量透明矩形的消息