我正在我的应用程序中实现一个邮件 Controller ,但我无法弄清楚为什么在单击保存草稿或删除草稿时它不会关闭。该窗口卡在电子邮件屏幕上,我也无法再次单击“取消”。

@IBAction func emailButtonTapped(_ sender: UIButton) {
    guard MFMailComposeViewController.canSendMail() else {
        if !MFMailComposeViewController.canSendMail() {
            print("Can not send email")
            return
        }
        return
    }


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

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    mailComposer.setToRecipients(["[email protected]"])
    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)
}

最佳答案

我成功解决了这个问题,这是一个非常愚蠢的问题!
一些代码在 emailButtonTapped 函数之外:

@IBAction func emailButtonTapped(_ sender: UIButton) {
    //guard MFMailComposeViewController.canSendMail() else {
        if !MFMailComposeViewController.canSendMail() {
            print("Can not send email")
            return
        }
    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    mailComposer.setToRecipients(["[email protected]"])
    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)
}

关于swift - 单击取消、保存草稿或删除草稿时无法关闭邮件 View Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47653635/

10-11 06:16