How to reject a mail view controller after clicking the send or cancel button

when sending mail, after pressing the send button or canceling the view button, the counters also remain there.

// swift 2.2; xcode 7.3.1;

if( MFMailComposeViewController.canSendMail() ) { print("Can send email.") } var subjectText = "Verification" var toReceipients = [" notorious.roman@gmail.com "] // var msgBody = "Verified" var mc:MFMailComposeViewController = MFMailComposeViewController() mc.mailComposeDelegate = self mc.setSubject(subjectText) mc.setMessageBody("Verified", isHTML: false) mc.setToRecipients(toReceipients) self.presentViewController(mc, animated: true, completion: nil) } func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { self.dismissViewControllerAnimated(true, completion: nil) } 
+5
source share
3 answers

I think @rmaddy will answer your question in his comment, however I am going to explain to you what is happening. You are trying to reject the UIViewController that represents the MFMailComposeViewController , not the MFMailComposeViewController .

As Apple points out in its documentation:

The mail message view controller does not turn off automatically. When the user selects the buttons to send email or cancel the interface, the mail message view controller calls the mailComposeController:didFinishWithResult:error: method of its delegate. Your implementation of this method should explicitly reject the view controller.

So you need to set the method like this:

  func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { // Dismiss the mail compose view controller. controller.dismissViewControllerAnimated(true, completion: nil) } 

Hope this helps you.

+6
source

Swift 3.0 update. Swift 4.0 update.

Let me add something to the discussion ...

In Swift 3 and 4, the delegate method has changed slightly; As he sent you now, there will be no effect and will not be triggered. Happened to me, drove me crazy!

Warning Xcode offers three fixes, but the first two can be misleading. This is just a tiny fix ...

Here the delegate method is fixed for Swift 3 and 4:

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

However, Victor's answer must be correct / accepted.

Enjoy it!

+13
source

has a 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) } 
+4
source

All Articles