我在创作
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
但是选择器还没有,应用程序因未捕获的异常'NSInvalidArgumentException'
而崩溃并出现错误 Terminating app,原因:'Application tried to present a nil modal view controller on target
。它在模拟器中工作正常但在 Device 中崩溃。
如何在 IOS 7 中使用 MFMailComposerViewController
。
最佳答案
您应该在尝试发送邮件之前检查 MFMailComposeViewController 是否能够发送您的邮件(例如,用户在 iOS 设备上没有任何邮件帐户)。
所以在你的 Objective-C 案例中:
MFMailComposeViewController *myMailCompose = [[MFMailComposeViewController alloc] init];
if ([MFMailComposeViewController canSendMail]) {
myMailCompose.mailComposeDelegate = self;
[myMailCompose setSubject:@"Subject"];
[myMailCompose setMessageBody:@"message" isHTML:NO];
[self presentViewController:myMailCompose animated:YES completion:nil];
} else {
// unable to send mail, notify your users somehow
}
swift 3:
let myMailCompose = MFMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
myMailCompose.mailComposeDelegate = self
myMailCompose.setSubject("Subject")
myMailCompose.setMessageBody("message", isHTML: false)
self.present(myMailCompose, animated: true, completion: nil)
} else {
// unable to send mail, notify your users somehow
}
关于objective-c - 应用程序在 IOS7 中的 MFMailComposeViewController 对象上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19051525/