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

3

12

while sending mail, after tapping send or cancel button view controller stays there and app stalls.

//swift 2.2 ; xcode 7.3.1 ;

  if( MFMailComposeViewController.canSendMail() ) {
            print("Can send email.")
        }

        var subjectText = "Verification"
        var toReceipients = ["[email protected]"]


        // 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)

    }
Tackle answered 15/5, 2016 at 19:11 Comment(1)
Call dismissViewControllerAnimated on controller, not self.Wrier
D
16

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.

Drucie answered 15/5, 2016 at 20:52 Comment(0)
F
19

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!

Fond answered 7/11, 2016 at 13:1 Comment(1)
It looks like the difference is the underbar in the function declaration.Unoccupied
D
16

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.

Drucie answered 15/5, 2016 at 20:52 Comment(0)
A
4

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)

}
Automate answered 5/9, 2017 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.