MFMailComposeViewController does not cancel

I am trying to implement MFMailComposeViewControllerwhen sending emails from my application. The problem is that after the presentation, MFMailComposeViewControllerit is not rejected by the Cancel or Send buttons, it only scrolls a bit.

Here is his view:

func mailButtonDidPressed {
        let emailTitle = "Test email"
        let messageBody = "some body bla bla bla"
        let toRecipents = "email@gmail.com"

        let emailComposer = MFMailComposeViewController()
        emailComposer.setSubject(emailTitle)
        emailComposer.setMessageBody(messageBody, isHTML: false)
        emailComposer.setToRecipients([toRecipents])
        emailComposer.mailComposeDelegate = self
        self.presentViewController(emailComposer, animated: true, completion: nil)
    }

and rejecting the delegate code:

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    switch (result) {
    case MFMailComposeResultSent:
        print("You sent the email.")
        break
    case MFMailComposeResultSaved:
        print("You saved a draft of this email")
        break
    case MFMailComposeResultCancelled:
        print("You cancelled sending this email.")
        break
    case MFMailComposeResultFailed:
        print("Mail failed:  An error occurred when trying to compose this email")
        break
    default:
        print("An error occurred when trying to compose this email")
        break
    }

    controller.dismissViewControllerAnimated(true, completion: nil)
}

I looked through StackOverflow and other services like this and couldn't find an answer.

+7
source share
7 answers

If anyone has this problem in Swift 3.0, I think there can be two methods for MFMailComposeViewController that look like the CORRECT method.

Make sure you use this method.

  func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true, completion: nil)
    }
+7

3

        composer.mailComposeDelegate = self as MFMailComposeViewControllerDelegate
+5

, completion :

extension UIViewController: MFMailComposeViewControllerDelegate {

    func sendEmail() {
      //send email
    }

    public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }

}
+1

, mailComposeControllermailComposeController , mailButtonDidPressedmailButtonDidPressed, , MFMailComposeViewController,

self.dismissViewControllerAnimated(true, completion: nil)

controller.dismissViewControllerAnimated(true, completion: nil)

0

:

emailComposer.delegate = self 

:

func mailButtonDidPressed {
        ...

        let emailComposer = MFMailComposeViewController()
       emailComposer.delegate = self 

...
    }
0

, .

MFMailComposeViewControllerDelegate

at the top, as if listing your inheritance, and then add material from other answers.

0
source

Swift 5

Remember to add both delegates:

emailComposer.mailComposeDelegate = self
emailComposer.delegate = self

If you add only one, it will not be rejected. Also be sure to implement the delegate method:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    dismiss(animated: true)
}
0
source

Source: https://habr.com/ru/post/1650676/


All Articles