how to dismiss mail view controller after tapping send or cancel button

…衆ロ難τιáo~ 提交于 2019-11-30 18:48:53

I think @rmaddy answer your question in his comment, nevertheless I going to explain you what's happening. You're trying to dismiss the UIViewController that presents the MFMailComposeViewController not the MFMailComposeViewController.

As Apple specify in his documentation:

The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController:didFinishWithResult:error: method of its delegate. Your implementation of that method must dismiss the view controller explicitly.

So you need to set the method in this way:

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

    // Dismiss the mail compose view controller.
    controller.dismissViewControllerAnimated(true, completion: nil)
}

I hope this help you.

Swift 4.0 Update. Swift 5.0 Update.

Allow me to add something to the discussion...

In Swift 4 and 5 the delegate method slightly changed; As it's posted by you now, won't do any effect and won't get called. It happened to me, drove me crazy!

The Xcode warning suggest three fixes but first two could be misleading. It's just a tiny fix...

Here's the delegate method fixed for Swift 3, 4 and 5:

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

        // Dismiss the mail compose view controller.
        controller.dismiss(animated: true, completion: nil)
    }

Still, Victor's answer should be the correct/accepted one.

Enjoy!

is had an Switch Statement that controls it for me:

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

    switch result.rawValue {
    case MFMailComposeResult.cancelled.rawValue :
        print("Cancelled")

    case MFMailComposeResult.failed.rawValue :
        print("Failed")

    case MFMailComposeResult.saved.rawValue :
        print("Saved")

    case MFMailComposeResult.sent.rawValue :
        print("Sent")



    default: break


    }

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!