Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

    }
like image 872
Parajuli Roman Avatar asked May 15 '16 19:05

Parajuli Roman


1 Answers

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!

like image 98
Helen Wood Avatar answered Nov 15 '22 17:11

Helen Wood