MFMailComposeViewController - iPad

RyanJM picture RyanJM · Feb 14, 2011 · Viewed 12.5k times · Source

I've setup a MFMailComposeViewController and it works just fine on the iPhone, but on the iPad it crashes, saying:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target...

So why would this create a nil modal view?

    MFMailComposeViewController *message = [[MFMailComposeViewController alloc] init];
    [message setMessageBody:@"My message here"  isHTML:NO];
    [message setToRecipients:[NSArray arrayWithObject:@"[email protected]"]];
    [message setSubject:@"Request Info"];
    message.mailComposeDelegate = self;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        message.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentModalViewController:message animated:YES];
    [message release];

Any ideas?

Answer

Vladimir picture Vladimir · Feb 14, 2011

Loos like MFMailComposeViewController was not created for some reason and thus has nil value. Check if it is nil before presenting it (although this workaround does not answer what went wrong here...).

You should also perform the check if mail composer can send mail before trying to create and present it using +canSendMail method (it will return NO for example if no mail account set up on device):

 if ([MFMailComposeViewController canSendMail]){
    // Create and show composer
 }
 else{
   // Show some error message here
 }