Search code examples
iosmfmailcomposeviewcontrollerpresentviewcontroller

iOS: crash on presenting MFMailComposerViewController


I have only a short piece of code that should present a mail view with certain details, and when called

 [self presentViewController:controller animated:YES completion:NULL];

The application crashes, saying Thread 1: breakpoint 1.1

I'm not sure whats causing this crash. I'm providing the code below.

NSString* encryptedText = [self getEncryptedText:noteText.text :cipherField.text];

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; controller.mailComposeDelegate = (id)self;

[controller setSubject:titleField.text];
[controller setMessageBody:encryptedText isHTML:NO];

[self presentViewController:controller animated:YES completion:NULL];

Thanks


Solution

  • Before you create the MFMailComposeViewController make sure to check the user has their mail set up on their device by adding

    if ([MFMailComposeViewController canSendMail]) {
    
        //Place your code here to create the controller and present
    
    }
    

    If you try to present a MFMailComposeViewController on a device that has no mail set up already, then the app will crash. Adding this condition will detect this.

    Hope this helps...