MFMailComposeViewController 仅在 iOS 9 中抛出错误

MFMailComposeViewController throws an error only in iOS 9

我无法在 iOS 9 模拟器中引发致命错误的情况下打开 MFMailComposeViewController。

相同的代码 (Objective C) 在 iOS 8.x 及更低版本中完美运行,但今天我安装了 Xcode 7 beta 5,当我 运行 iOS 9 Simulator 上的应用程序,我得到一个标题为“MailCompositionService 意外退出”的对话框,当我查看错误报告时,我看到:

Application Specific Information: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7fd314280b10'

terminating with uncaught exception of type NSException abort() called CoreSimulator 179 - Device: iPhone 6 - Runtime: iOS 9.0 (13A4325c) - DeviceType: iPhone 6

错误发生在邮件撰写视图出现时。它冻结了几秒钟,然后出现错误对话框。

打开邮件撰写视图的代码是:

if ([MFMailComposeViewController canSendMail])
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Comment title"];
    [picker setMessageBody:@"Comment description" isHTML:NO];
                                        
    [self.window.rootViewController presentModalViewController:picker animated:YES];
    [picker release];
}

如果知道的话,在应用程序崩溃之前,mailComposeController:didFinishWithResult:error: 被调用 result = MFMailComposeResultCancellederror = nil.

我希望得到有关如何查找此错误原因的提示。谢谢!

你应该使用: [self.window.rootViewController presentViewController:picker animated:YES completion:NULL]; presentModalViewController 已弃用,因为 ios6 已经被 presentViewController:animated:completion: 取代 IE: - (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated NS_DEPRECATED_IOS(2_0, 6_0);

问题出在模拟器上,在真实设备上邮件编辑器工作正常。

我不知道它为什么会发生或者我是怎么发现它的,崩溃似乎是通过在导航栏的外观代理中设置 NSFontAttributeName 产生的,如果我取消注释该行应用程序崩溃。

    NSDictionary* format = @{
                         NSForegroundColorAttributeName:[UIColor whiteColor],
                         //NSFontAttributeName: [UIFont boldSystemFontOfSize:20],
                         };

[[UINavigationBar appearance] setTitleTextAttributes:format];

请@Sleiman 试试看这是否也能解决您的问题。

根据 Apple 开发者论坛,更多详细信息为 here

The simulator doesn't support mail. You should likely try testing mail functionality in a device.

作为解决此问题的简单方法,您可以使用 "mailto" 协议,它将:

  • 不会使应用程序(设备和模拟器)崩溃
  • 如果设备未使用任何邮件帐户登录,则提示用户登录

swift 中的示例:

Swift 3.0

let mailRecipient = "support@abc.com"
let mailSubject = "Help with ABC for iOS"
let mailBody = "xxx"

let mailTo = "mailto:\(mailRecipient)?subject=\(mailSubject)&body=\(mailBody)"

guard let escapedMailTo = mailTo.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
    NSLog("Invalid mail to format")
    return
}

guard let url = NSURL(string: escapedMailTo) else {
    NSLog("Invalid mail to format: \(escapedMailTo)")
    return
}

UIApplication.sharedApplication().openURL(url)

Swift 2.3

let mailRecipient = "support@abc.com"
let mailSubject = "Help with ABC for iOS"
let mailBody = "xxx"

let mailTo = "mailto:\(mailRecipient)?subject=\(mailSubject)&body=\(mailBody)"

guard let escapedMailTo = mailTo.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) else {
    NSLog("Invalid mail to format")
    return
}

guard let url = NSURL(string: escapedMailTo) else {
    NSLog("Invalid mail to format: \(escapedMailTo)")
    return
}

UIApplication.sharedApplication().openURL(url)