Search code examples
iosswiftemailios13mfmailcomposeviewcontroller

MFMailComposeViewController does not send email or dismiss


I'm using MFMailComposeViewController in a view controller with the following code:

if !MFMailComposeViewController.canSendMail() {
    return
}
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.mailComposeDelegate = self
present(mailComposeViewController, animated: true)

And:

extension MyViewController : MFMailComposeViewControllerDelegate {
    private func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true, completion: nil)
    }
}

The MFMailComposeViewController shows as expected, but has the following behavior:

  • Cancel button either does nothing (if the message has not been edited) or shows the "Delete Draft"/"Save Draft" action sheet, none of the options of which dismiss the MFMailComposeViewController
  • The send button does nothing, whether or not it's disabled (no recipient set) or enabled (recipient set)
  • The view can be dismissed by swiping it down (new iOS 13 modal behavior)

This is Xcode 11.2, iOS 13.2, Swift 4.

How can I fix this?


Solution

  • In this code

    extension MyViewController : MFMailComposeViewControllerDelegate {
        private func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            controller.dismiss(animated: true, completion: nil)
        }
    }
    

    Delete the keyword private. It hides the method from Cocoa so that it will never be called.