objective-c - 应用程序在 IOS7 中的 MFMailComposeViewController 对象上崩溃

标签 objective-c ipad

我在创作

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/

相关文章:

iphone - 捕获 CGRect 不会提供正确的图像。该怎么办?

ios - 无法创建 lldb private : Xcode can't run the app on iphone

ios - 我需要延迟 UITableView 的调用,直到其他方法完成执行

iphone - 沿圆形UIImage添加白色边框

iphone - iOS - 如何执行 Clear 应用程序等视觉效果?

iphone - 两个 View 多个 UIPickerViews 单个导出

iphone - 如何为 iPhone 5 屏幕分辨率迁移应用程序?

objective-c - CFSTR内存管理

ios - 应用程序在模拟器中完美运行,但在 iPod Touch 上运行时出现 "Undefined Symbols"错误

ios - 在 IOS 上使用 CollectionView 的弹出窗口