无法获取应用程序来关闭邮件撰写视图控制器&返回第一个屏幕

时间:2017-09-18 18:59:09

标签: ios swift mfmailcomposeviewcontroller

我正在使用Swift书来尝试学习编码。我添加了委托方法来关闭视图,但它不起作用。我在这里错过了什么?

@IBAction func emailButtonTapped(_ sender: UIButton) {

    if !MFMailComposeViewController.canSendMail() {
        print("Can not send mail")
        return
    }

    guard MFMailComposeViewController.canSendMail() else { return }

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    mailComposer.setToRecipients(["example@example.com"])
    mailComposer.setSubject("Look at this")
    mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)

    present(mailComposer, animated: true, completion: nil)

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

}

1 个答案:

答案 0 :(得分:1)

您只需要在IBAction

之外声明委托功能
@IBAction func emailButtonTapped(_ sender: UIButton) {
    if !MFMailComposeViewController.canSendMail() {
        print("Can not send mail")
        return
    }

    guard MFMailComposeViewController.canSendMail() else { return }

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    mailComposer.setToRecipients(["example@example.com"])
    mailComposer.setSubject("Look at this")
    mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)

    present(mailComposer, animated: true, completion: nil)
}

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