swiftdelegatesmfmailcomposeviewcontrollermfmailcomposer

SWIFT - mailComposeDelegate not called : cancel of MFMailComposeViewController doesn't work


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

Solution

  • 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