Search code examples
objective-cmfmailcomposeviewcontrollerios10presentviewcontroller

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


I am implementing MFMailComposeViewController in my application when I click on my mail button I am get the below error.

Application tried to present a nil modal view controller on target strong text

Here's my code:

    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];

What's wrong with my code. Please help


Solution

  • If device has not configured mail or using simulator can lead to crash or exception.

    mailComposer = [[MFMailComposeViewController alloc] init];
    

    above line of code can cause problem. in simulator initializer method may return NULL or nil.

    so, just check for canSendMail before presenting modal viewController:

    Like,

        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];
    }