presentViewController 在 iPad 时崩溃,但在 iPhone 时不会崩溃

presentViewController crashes with iPad but not with iPhone

我的 iPhone 应用程序被 TestFlight 拒绝,因为它使 iPad 崩溃了。有问题的代码试图显示一个用于发送电子邮件的控制器。我已将代码缩减为一个小而简单的示例,它在我的 iPhone 5c 运行 iOS 9.3.1 上按预期工作,但使我的 iPad 2 [=26= 崩溃] iOS 9.3.1:

- (void)viewDidLoad {
  [super viewDidLoad];
  [self displayComposerSheet];
}

-(void)displayComposerSheet {
  // Create e-mail interface
  MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
  picker.mailComposeDelegate = self;
  [picker setSubject:@"iPad crash test"];

  // Add recipients
  NSArray *toRecipients = [NSArray arrayWithObject:@"email@somewhere.com"];
  [picker setToRecipients:toRecipients];

  // Fill body
  NSString *emailBody = @"A short test of iPad crashes";
  [picker setMessageBody:emailBody isHTML:NO];

  // Show interface - iPad crashes here but iPhone is ok
  [self presentViewController:picker animated:YES completion:nil];
}

错误信息是:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target .'

我花了一些时间来解决这个问题,但一直没能解决。任何想法或建议将不胜感激!

为了发送电子邮件,应该在设备上定义一个电子邮件帐户。如果未设置帐户,MFMailComposeViewController 将产生问题并崩溃。

最好检查您的设备是否能够通过 canSendMail 方法发送电子邮件。

if ([MFMailComposeViewController canSendMail])
    [self presentViewController:picker animated:YES completion:nil];