iosswift3mfmailcomposer

MFMailComposeViewControllerDelegate method not getting invoked after Swift 3.0 migration


My app has email compose feature and it was working perfectly on Swift 2.2. Recently I migrated code to Swift 3.0 and stuck with this issue. Sharing my code snippet below:

import MessageUI

class ViewController: UIViewController,     MFMailComposeViewControllerDelegate {
 func sendEmail() {
    if MFMailComposeViewController.canSendMail() {
      let mail = MFMailComposeViewController()
      mail.mailComposeDelegate = self
      mail.setToRecipients(["someone@somewhere.com"])
      mail.setSubject("Sending you an in-app e-mail...")
      mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
      self.present(mail, animated: true)
    } else {
        //  handle failure
    }
  }

 func mailComposeController(_ controller:    MFMailComposeViewController, didFinishWith result: MFMailComposeResult,  error: Error?) {

    controller.dismiss(animated: true)
  }

 @IBAction func mailClicked(_ sender: AnyObject) {    
    sendEmail()
  }
 }

I have set mailComposeDelegate to self, put _ in delegate method and tried all solutions found on search. But could not resolve the issue.

Any help is greatly appreciated.


Solution

  • It is a known issue in Xcode 8. Following workaround worked for me:

    @objc(mailComposeController:didFinishWithResult:error:)
      func mailComposeController(_ controller: MFMailComposeViewController,  didFinishWith result: MFMailComposeResult, error: NSError?) {
     controller.dismiss(animated: true, completion: nil)
    }