【问题标题】:presentViewController crashes with iPad but not with iPhonepresentViewController 在 iPad 上崩溃,但在 iPhone 上不崩溃
【发布时间】:2016-05-04 17:58:10
【问题描述】:

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

- (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];
}

错误信息是:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图在目标上呈现 nil 模态视图控制器。”

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

【问题讨论】:

  • picker nil?阅读MFMailComposeViewController 的文档。在尝试使用此类之前,您需要检查设备是否可以发送电子邮件。

标签: objective-c iphone ipad presentviewcontroller


【解决方案1】:

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

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

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

【讨论】:

  • displayComposerSheet 方法的全部内容应该在if 语句中。如果设备无法发送电子邮件,甚至没有理由创建和设置 MFMailComposeViewController 实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
  • 2021-11-05
  • 2012-06-27
  • 2012-11-26
相关资源
最近更新 更多