ios:应用程序尝试在目标上显示nil模态视图控制器

Bha*_*jan 29 ios7 mfmailcomposeviewcontroller

我正在开发一个应用程序,要求是在UIAlertView的按钮上打开电子邮件编辑器.

从UITextView复制邮件的邮件正文中的邮件.我正在使用以下代码片段:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)
  {
      // opening message composer
  }
else
  {
   MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Test mail"];
    [picker setMessageBody:messageBody.text isHTML:YES];
    [self presentViewController:picker animated:YES completion:NULL];
  }
}
 // mail compose delegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
      [self dismissViewControllerAnimated:YES completion:NULL];
}
Run Code Online (Sandbox Code Playgroud)

但问题是我得到错误,说应用程序试图在目标上呈现一个零模态视图控制器.我们如何在ios 7中打开默认邮件编辑器?

Tos*_*lji 75

根据Apple,您应该检查MFMailComposeViewController是否能够在发送之前发送您的邮件

if ([MFMailComposeViewController canSendMail]) {
     MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Test mail"];
    [picker setMessageBody:messageBody.text isHTML:YES];
    [self presentViewController:picker animated:YES completion:NULL];
}
Run Code Online (Sandbox Code Playgroud)

参考:Apple Dev url



roc*_*ift 11

Swift 4版本

guard MFMailComposeViewController.canSendMail() else {
    print("Mail services are not available")
    return
}
sendEmail()
Run Code Online (Sandbox Code Playgroud)


pro*_*per 10

忘记设备设置中的邮件帐户配置也可能导致此错误.重新检查您的设备中是否配置了邮件帐户.