SWIFT - 未调用 mailComposeDelegate:取消 MFMailComposeViewController 不起作用

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

在同一个viewcontroller上,我们可以通过发邮件或者短信的方式向朋友发送信息。 应用程序中的短信完全有效。但是对于电子邮件,电子邮件应用程序会在我的应用程序中打开,其中包含我要求写的所有信息,但无法通过按下取消来关闭它,没有任何反应。 我试过 mc.mailComposeDelegate = self 或 mc.delegate = self 并且 MFMailComposeViewControllerDelegate 也在顶部。 我在互联网上查看了所有内容,没有找到任何解释。 mailComposeController 永远不会被调用! 你有什么想法吗?

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

我让它工作没有任何问题,但我的委托方法看起来与你的有点不同:

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

你可以试试这个。

你需要设置 mc.mailComposeDelegate = self 而不是 mc.delegate = self

首先使用

mc.mailComposeDelegate = self

而不是

mc.delegate = self

此外,在 Swift 3 的情况下,委托方法以某种方式更新为:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?){
    controller.dismiss(animated: true, completion: nil)
}

对于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
    }