Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFMailComposeViewController won't dismiss

I have the following code in my app to send an e-Mail:

let ToRecipents = ["recipient here"]
        let subject = "subject here"
        let MessageBody = "message here"

        let mc : MFMailComposeViewController = MFMailComposeViewController()

        mc.mailComposeDelegate = self


        mc.setToRecipients(ToRecipents)
        mc.setSubject(subject)
        mc.setMessageBody(MessageBody, isHTML: false)

        self.present(mc, animated: true, completion: nil)

    }

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

        controller.dismiss(animated: true, completion: nil)

    }

I can't seem to get MFMailComposeViewController to dismiss, what am I doing wrong??

like image 702
DanielPetters386 Avatar asked Dec 10 '25 12:12

DanielPetters386


1 Answers

In a general way when delegate methods are not called:
• Check if the delegate is set. You do it correctly with mc.mailComposeDelegate = self
• Check if the delegate method you set is compliant with the protocol.
• Check if the delegate method is correctly implemented (that's where lies your issue) in that object you passed as delegate. Read the doc, or remove the method declaration and let the compiler/IDE/XCode autocomplete it for you. In Swift between various tutorials they are often issues because Swift 1, Swift 2, Swift 3/4 have renamed the methods and causing issues, tutorial being focused on a Swift version only.

Your method:

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

The method from the documentation:

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

Since they are not the same, and the iOS SDK should check the if the delegate responds to the correct method, then yours shouldn't be called because it doesn't match.

like image 169
Larme Avatar answered Dec 12 '25 01:12

Larme