iosobjective-cuiviewcontrollermfmailcomposeviewcontrollermfmailcomposer

iOS: MFMailComposeViewController not getting closed


I am using MFMailComposeViewController in my application to compose a feedback E-Mail. The MFMailComposeViewController gets displayed, but can't be closed.

Method used to open the MFMailComposeViewController modal window:

-(IBAction) feedbackBtnClicked:(id)sender {

    // Dismiss the Old View Controller
    [self dismissViewControllerAnimated:NO completion:NULL];

    // Present the New View Controller
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
        mail.mailComposeDelegate = self;
        [mail setSubject:@"Sample Subject"];
        [mail setMessageBody:@"Here is some main text in the email!" isHTML:NO];
        [mail setToRecipients:@[@"example@mail.com"]];

        [self presentViewController:mail animated:YES completion:NULL];
    }
    else
    {
        NSLog(@"This device cannot send email");
    }
}

Here is what happens, when clicking on the buttons:

Senden (Send) - The E-Mail gets sent, but the modal window stays open; clicking on that button multiple times results in sending multiple E-Mails without the modal window ever getting closed. Abbrechen (Cancel) - Nothing happens

How to dismiss make sure the MFMailComposeViewController gets dismissed after clicking on those buttons?

Screenshot


Solution

  • You need to implement the MFMailComposeViewControllerDelegate method mailComposeController:didFinishWithResult:error:, and dismiss the mail view controller…

    - (void)messageComposeViewController:(MFMessageComposeViewController *)controller
                     didFinishWithResult:(MessageComposeResult)result
    {
      [self dismissViewControllerAnimated:YES completion:NULL];
    }