【问题标题】:Sending Email using mailto: URLs使用 mailto 发送电子邮件:URL
【发布时间】:2012-01-26 01:36:14
【问题描述】:

有人可以帮我编写以下代码吗?对于在 iOS 中发送电子邮件,下面的代码是一个好代码还是我应该使用 MFMailComposeViewController 而不是这个?:

NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

它是发送邮件的可靠代码吗?

【问题讨论】:

  • 您不需要在文字字符串上使用stringWithString:。它已经是一个字符串。 (事实上​​,拨打stringWithString: 的理由很少。)

标签: ios cocoa-touch cocoa mfmailcomposeviewcontroller


【解决方案1】:

如果这是针对 IOS 3.0+ 那么 MFMailCompseViewController

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

        MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
        controller.mailComposeDelegate = self;
        [controller setSubject:@"My Subject"];
        [controller setMessageBody:@"Hello there." isHTML:NO]; 
        if (controller) [self presentModalViewController:controller animated:YES];
        [controller release];

然后用户做工作,你及时得到委托回调:

 - (void)mailComposeController:(MFMailComposeViewController*)controller  
              didFinishWithResult:(MFMailComposeResult)result 
                            error:(NSError*)error;
 {
      if (result == MFMailComposeResultSent) {
          NSLog(@"sent");
        }
    [self dismissModalViewControllerAnimated:YES];
 }

【讨论】:

  • 谢谢dj..又一个问题>... 出现AB界面时,是否可以同时从通讯录中选择多个联系人?
【解决方案2】:

你真的应该使用 MFMailComposeViewController。它让您始终留在应用程序中,并使您的代码更具可读性。

【讨论】:

    【解决方案3】:

    为了扩展所提供的答案,我想补充一点,mailto 方法有一个好处,那就是您不必检查用户是否能够发送电子邮件。如果用户不能这样做,它会通过电子邮件向导提示用户,允许他/她使用默认的苹果邮件应用程序设置电子邮件帐户。

    如果是MFMailComposeViewController,您应该经常检查用户是否可以使用canSendMail 方法发送电子邮件,并采取相应措施。

    我还想指出,mailto 方法不允许您以直接的方式设置委托,从而使错误处理更加棘手。

    【讨论】:

      猜你喜欢
      • 2016-03-15
      • 2023-03-13
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多