MFMailComposeViewController 视图不会第二次关闭

MFMailComposeViewController view does not dismiss 2nd time

我有一个我认为是独特的问题。我无法关闭我的电子邮件 window。我正在使用 Xcode 8.

电子邮件在我第一次打开时正确关闭,但如果我再次打开它就不会。如果我按 "Cancel",它不会给我 "Delete Draft" 的选项。如果我按 "Send" 发送电子邮件,但 window 不会关闭。

我的代码如下。 mailComposeController 第一次被正确调用,但它永远不会被第二次调用。有人对我缺少的东西有任何想法吗?

let mail = MFMailComposeViewController()
func sendEmail(body: String, subject: String) {
    if MFMailComposeViewController.canSendMail() {
        mail.mailComposeDelegate = self

        mail.setSubject(subject)
        mail.setMessageBody("\(body)", isHTML: false)

        if let data = (body as NSString).data(using: String.Encoding.utf8.rawValue){
            //Attach File
            mail.addAttachmentData(data, mimeType: "text/plain", fileName: "data.txt")
        }

        present(mail, animated: true)
    } else {
        // show failure alert
    }
}

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

您每次都需要创建一个新的MFMailComposeViewController。将你的 mail 声明移到 sendEmail 中工作......

func sendEmail(body: String, subject: String) {
    if MFMailComposeViewController.canSendMail() {

       // Create a new MFMailComposeViewController…
       let mail = MFMailComposeViewController()

        mail.mailComposeDelegate = self

        mail.setSubject(subject)
        mail.setMessageBody("\(body)", isHTML: false)

        if let data = (body as NSString).data(using: String.Encoding.utf8.rawValue){
            //Attach File
            mail.addAttachmentData(data, mimeType: "text/plain", fileName: "data.txt")
        }

        present(mail, animated: true)
    } else {
        // show failure alert
    }
}

至于为什么……?