MFMailComposeViewController仅在iOS 9中引发错误
我不能让MFMailComposeViewController打开,而不会在iOS 9模拟器中引发致命错误。
相同的代码(Objective C)在iOS 8.x及更低版本中工作正常,但是今天我安装了Xcode 7 beta 5,当我在iOS 9 Simulator上运行应用程序时,出现一个名为“MailCompositionService意外退出”的对话框,错误报告,我看到:
应用程序特定信息:***由于未捕获的exception“NSInvalidArgumentException”而终止应用程序,原因:' – [__ NSArrayI isEqualToString:]:无法识别的select器发送到实例0x7fd314280b10'
以NSExceptiontypes的未捕获exception终止(称为CoreSimulator 179) – 设备:iPhone 6 – 运行时:iOS 9.0(13A4325c) – 设备types: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
= MFMailComposeResultCancelled
, error
= nil
。
我会很感激提示如何find这个错误的原因。 谢谢!
问题是与模拟器,在真正的设备邮件作曲工工作正常。
根据苹果开发者论坛,更多细节在这里 。
模拟器不支持邮件。 您应该尝试在设备中testing邮件function。
你应该使用: [self.window.rootViewController presentViewController:picker animated:YES completion:NULL];
presentModalViewController是DEPRECATED,因为ios6是和已被replacepresentViewController:animated:completion:
即: – (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尝试,看看这是否也解决了你的问题。
作为一个简单的解决这个问题,你可以使用“mailto”协议,它将:
- 不会崩溃的应用程序(设备和模拟器)
- 如果设备没有用任何邮件账户login,提示用户login
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)