【问题标题】:Application tried to present a nil modal view controller on target in iOS 10?应用程序试图在 iOS 10 的目标上呈现一个 nil 模式视图控制器?
【发布时间】:2016-09-21 06:56:12
【问题描述】:

当我点击我的邮件按钮时,我正在我的应用程序中实现 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];

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

【问题讨论】:

    标签: objective-c mfmailcomposeviewcontroller ios10 presentviewcontroller


    【解决方案1】:

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

    mailComposer = [[MFMailComposeViewController alloc] init];
    

    上面的代码行可能会导致问题。在模拟器中初始化方法可能返回NULLnil

    所以,在呈现模态视图控制器之前检查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];
    }
    

    【讨论】:

    • @visible interface for MFMailcomposerViewController 声明选择器“canSendMail”我收到此错误
    • 是的,在模拟器中它不会进入IF条件,检查真实设备@User558
    • 是的,我只检查真实设备。收到这样的警报,file:///Users/unxusmac1/Desktop/Scr​​een%20Shot%202016-09-21%20at%202.14.47%20PM.png
    • 无邮件账户请设置邮件账户以发送邮件
    • 发送邮件需要设备中配置的邮件账户,在iphone@User558的邮件应用中配置邮件后尝试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 2016-12-22
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    相关资源
    最近更新 更多