简体   繁体   中英

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

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...

Try this code.

if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@""]];
        [composeViewController setSubject:@"Hi Test"];
        [composeViewController setMessageBody:@"Hello Test" isHTML:YES];

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM