应用程序试图在 iOS 10 中在目标上呈现一个 nil 模态视图控制器?

Application tried to present a nil modal view controller on target in iOS 10?

当我点击我的邮件按钮时,我在我的应用程序中实现了 MFMailComposeViewController,我收到以下错误。

应用程序试图在目标强文本上呈现一个无模态视图控制器

这是我的代码:

    NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
    NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

    NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

    mailComposer = [[MFMailComposeViewController alloc]init];
    mailComposer.navigationBar.tintColor = [UIColor blackColor];
    [mailComposer.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor blackColor]}];
    mailComposer.mailComposeDelegate = self;

    [mailComposer setSubject:@"Co - Contact Us"];

    [mailComposer setToRecipients:@[@"contact@co.org"]];
    NSString *supportText = @"Enter your comment here:\n\n\n\n\n";
    supportText = [supportText stringByAppendingString:[NSString stringWithFormat:@"iOS Version: %@\nCorner Version:%@\nBuild:%@\n\n",iOSVersion,version, build]];
    [mailComposer setMessageBody:supportText isHTML:NO];

    [self presentViewController:mailComposer animated:YES completion:nil];

我的代码有什么问题。请帮助

如果设备没有配置邮件或使用模拟器会导致崩溃或异常。

mailComposer = [[MFMailComposeViewController alloc] init];

以上代码行可能会导致问题。在模拟器初始化方法中可能 return NULLnil.

所以,在呈现模态 viewController:

之前检查 canSendMail

喜欢,

    NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
    NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

    NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

    mailComposer = [[MFMailComposeViewController alloc]init];

    if ([MFMailComposeViewController canSendMail] && mailComposer) {

       mailComposer.navigationBar.tintColor = [UIColor blackColor];
       [mailComposer.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
       mailComposer.mailComposeDelegate = self;

       [mailComposer setSubject:@"Corner - Contact Us"];

       [mailComposer setToRecipients:@[@"contact@corner.org"]];
       NSString *supportText = @"Enter your comment here:\n\n\n\n\n";
       supportText = [supportText stringByAppendingString:[NSString stringWithFormat:@"iOS Version: %@\nCorner Version:%@\nBuild:%@\n\n",iOSVersion,version, build]];
       [mailComposer setMessageBody:supportText isHTML:NO];

      [self presentViewController:mailComposer animated:YES completion:nil];
}