akash23a
akash23a

Reputation: 127

Can't dismiss mail composer using dismiss(animated: true, completion: nil) and dismissViewControllerAnimated doesn't come up as an option

I've imported MessageUI and created an extension to include MFMailComposeViewControllerDelegate which includes dismiss(animated: true, completion: nil) and this doesn't close the VC that pops up to send the message however this is the only option that comes up when typing dismiss. I can't tell if that's what's required with the a newer Swift version as all other examples show dismissViewControllerAnimated. Not sure what I'm missing?

Action Button Code

    @IBAction func sendTapped(_ sender: UIButton) {

    let toRecipient = ["\(Constants.SubmitFeedback.feedbackEmail)"]

    let mc = MFMailComposeViewController()
    mc.mailComposeDelegate = self

    mc.setToRecipients(toRecipient)
    mc.setSubject("Feedback for Gallery App")

    mc.setMessageBody("Name: \(String(describing: nameTextField.text!)) \nEmail: \(String(describing: emailTextField.text!)) \n\nMessage: \n\(String(describing: messageTextField.text!))", isHTML: false)

    self.present(mc, animated: true, completion: nil)

}

Extension with 'dismiss' command

extension FeedViewController: MFMailComposeViewControllerDelegate {

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
    }

    dismiss(animated: true, completion: nil)
}

}

Upvotes: 0

Views: 125

Answers (1)

glotcha
glotcha

Reputation: 578

from the docs

call this method on the presented view controller

i'm sure we all made the same mistake first time we used that api :-)

Upvotes: 0

Related Questions