【问题标题】:SWIFT - mailComposeDelegate not called : cancel of MFMailComposeViewController doesn't workSWIFT - 未调用 mailComposeDelegate:取消 MFMailComposeViewController 不起作用
【发布时间】:2016-05-15 14:32:59
【问题描述】:

在同一个视图控制器上,我们可以发送电子邮件或短信向朋友发送信息。 应用程序中的短信完全有效。但是对于电子邮件,电子邮件应用程序会在我的应用程序内打开,其中包含我要求编写的所有信息,但是通过按下取消来关闭它是不可能的,没有任何反应。 我尝试了 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)
}

【问题讨论】:

    标签: swift delegates mfmailcomposeviewcontroller mfmailcomposer


    【解决方案1】:

    我让它正常工作,但我的委托方法看起来和你的有点不同:

    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

    【讨论】:

    • 天哪,它有效,我不知道你是谁,但谢谢你 1000 次!祝你有美好的一天!
    【解决方案2】:

    首先使用

    mc.mailComposeDelegate = self
    

    而不是

    mc.delegate = self
    

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

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

    【讨论】:

    • 这拯救了我的一天
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 2021-10-24
      • 2011-05-05
      • 2014-11-06
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      相关资源
      最近更新 更多