Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't dismiss MFMailComposeViewController in Swift 3.0

The MFMailComposeViewController cannot be dismissed after pressing cancel or send button. I have added MFMailComposeViewControllerDelegate in my class but still, it's not working?

Here is my code:

func sendEmail() {
    let MailVC = MFMailComposeViewController()
    MailVC.mailComposeDelegate = self
    MailVC.setToRecipients(["\(emailLabel?.text)"])
    MailVC.setSubject("Set your subject here!")
    MailVC.setMessageBody("Hello this is my message body!", isHTML: false)
    // Present the view controller modally.
    self.present(MailVC, animated: true, completion: nil)
}

func mailComposeController(controller: MFMailComposeViewController,
                           didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)
}
like image 855
Naveen Paulsingh Avatar asked Aug 07 '17 07:08

Naveen Paulsingh


1 Answers

Delegate method signature is wrong. You are missing _ before controller parameter. Try this.

public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)
}

And make sure this.

class ViewController: UIViewController ,MFMailComposeViewControllerDelegate
like image 137
Bilal Avatar answered Sep 17 '22 14:09

Bilal