David Seek
David Seek

Reputation: 17132

Swift / how to make MFMailComposeViewController disappear

I'm sending an email from UITextfields by UIButton with the following parameters.

    let mc: MFMailComposeViewController = MFMailComposeViewController()
    mc.mailComposeDelegate = self
    mc.setSubject("Neue Reservierungsanfrage")
    mc.setMessageBody(messageBody, isHTML: false)
    mc.setToRecipients(["[email protected]"])
    self.presentViewController(mc, animated: true, completion: nil)

No matter if I'm pressing cancel or send, the Email-sending Screen does not disappear.

How am I able to let the screen disappear AND push a confirmation popup once the user pressed "send"?

Help is very appreciated.

Upvotes: 0

Views: 296

Answers (1)

Yunus Eren Güzel
Yunus Eren Güzel

Reputation: 3088

You need to implement the protocol MFMailComposeViewControllerDelegate with the method mailComposeController(_:didFinishWithResult:error:)

Inside this method you can dismiss mail composer view controller. However I am not sure if it is possible to show a confirmation box to user since this button is handled by mail composer view controller.

set delegate of mail composer object:

let mc: MFMailComposeViewController = MFMailComposeViewController()
mc.delegate = self
mc.mailComposeDelegate = self
mc.setSubject("Neue Reservierungsanfrage")
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(["[email protected]"])
self.presentViewController(mc, animated: true, completion: nil)

method:

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    controller.dismissViewControllerAnimated(true, completion: nil)
}

Don't forget to add MFMailComposeViewControllerDelegate to your class definition.

Upvotes: 1

Related Questions