iosswiftxcodemfmailcomposeviewcontroller

MFMailComposeViewController is not dismissing - Swift, Xcode


I'm currently trying to implement MFMailComposeViewController. Everything is working fine, except after sending an email or clicking cancel, the MFMailComposeViewController does not dismiss. I've looked at similar StackOverflow threads and attempted to implement those solutions, but they have not resolved my issue. I feel like the issue may not lie within the func mailComposeController, but within the rest of my code. Any help would be greatly appreciated.

My code:

   if MFMailComposeViewController.canSendMail() {
        let vc = MFMailComposeViewController()
        vc.delegate = self
        vc.mailComposeDelegate = self
        vc.setSubject("Contact Us/Feedback")
        vc.setToRecipients(["test@gmail.com"])
        present(vc, animated: true)
    }
    else {
        if let url = URL(string: "https://www.google.com") {
        let safariVC = SFSafariViewController(url: url)
                
        present(safariVC, 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)
    }
}

Thanks!


Solution

  • Change

    func mailComposeController(controller: MFMailComposeViewController,
         didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    

    To

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