7

On the same viewcontroller, we can send an email or a text message to send an information to a friend. The text message in app fully works. But for the email, the email app opens inside my app with all the informations I asked to write but it's impossible to dismiss it by pushing cancel, nothing happens. I tried mc.mailComposeDelegate = self or mc.delegate = self and MFMailComposeViewControllerDelegate is at the top too. I looked everything on internet, I didn't find any explanation. mailComposeController is never called! Do you have any idea ?

class inviteAFriendViewController: UIViewController, MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate {

@IBAction func emailButtonDidTouch(sender: AnyObject) {
    sendEmail()
}

func sendEmail() {
    let emailTitle = "text"
    let messageBody = "text"
    let toRecipents = [""]

    let mc = MFMailComposeViewController()

    //mc.mailComposeDelegate = self

    mc.delegate = self

    mc.setSubject(emailTitle)
    mc.setMessageBody(messageBody, isHTML: false)
    mc.setToRecipients(toRecipents)

    presentViewController(mc, animated: true, completion: nil)
}

func mailComposeController(controller2: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    switch result.rawValue {
    case MFMailComposeResultCancelled.rawValue:
        print("Mail cancelled")
        controller2.dismissViewControllerAnimated(true, completion: nil)
    case MFMailComposeResultSaved.rawValue:
        print("Mail saved")
        controller2.dismissViewControllerAnimated(true, completion: nil)
    case MFMailComposeResultSent.rawValue:
        print("Mail sent")
        controller2.dismissViewControllerAnimated(true, completion: nil)
    case MFMailComposeResultFailed.rawValue:
        print("Mail sent failure.")
        controller2.dismissViewControllerAnimated(true, completion: nil)
    default:
        break
    }
    controller2.dismissViewControllerAnimated(true, completion: nil)
}
Oscar Falmer
  • 1,771
  • 1
  • 24
  • 38

3 Answers3

17

I got it working without any problems, but my delegate method looks a little bit different to yours:

func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError?) {
    switch result.rawValue {
    case MFMailComposeResultCancelled.rawValue:
        print("Mail cancelled")
    case MFMailComposeResultSaved.rawValue:
        print("Mail saved")
    case MFMailComposeResultSent.rawValue:
        print("Mail sent")
    case MFMailComposeResultFailed.rawValue:
        print("Mail sent failure: %@", [error.localizedDescription])
    default:
        break
    }
    self.dismissViewControllerAnimated(true, completion: nil)
}

you may try this one.

And you need to set mc.mailComposeDelegate = self and not the mc.delegate = self

Jamal
  • 229
  • 2
  • 5
10

First of all use

mc.mailComposeDelegate = self

instead of

mc.delegate = self

Moreover, in case of Swift 3, delegate method is somehow updated which is:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?){
    controller.dismiss(animated: true, completion: nil)
}
Ghulam Rasool
  • 3,996
  • 2
  • 27
  • 40
2

For Swift 4:

switch result.rawValue {
    case MFMailComposeResult.cancelled.rawValue:
        print("Mail cancelled")
    case MFMailComposeResult.saved.rawValue:
        print("Mail saved")
    case MFMailComposeResult.sent.rawValue:
        print("Mail sent")
    case MFMailComposeResult.failed.rawValue:
        print("Mail sent failure: \(error?.localizedDescription)")
    default:
        break
    }
Moe
  • 43
  • 5