Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
}
like image 226
Oscar Falmer Avatar asked May 15 '16 14:05

Oscar Falmer


3 Answers

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

like image 167
Jamal Avatar answered Nov 25 '22 05:11

Jamal


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)
}
like image 35
Ghulam Rasool Avatar answered Nov 25 '22 06:11

Ghulam Rasool


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
    }
like image 44
Moe Avatar answered Nov 25 '22 05:11

Moe