RVJ
RVJ

Reputation: 99

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(["[email protected]"])
      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.

Upvotes: 1

Views: 534

Answers (1)

RVJ
RVJ

Reputation: 99

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)
}

Upvotes: 7

Related Questions