5

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

电子邮件在我第一次打开时会正确关闭,但如果我再次打开它就不会。如果我按“取消”,它不会给我“删除草稿”的选项。如果我按“发送”,则会发送电子邮件,但窗口不会关闭。

我的代码如下。第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)
}
4

1 回答 1

8

MFMailComposeViewController您每次都需要创建一个新的。将您的mail声明移到sendEmail作品中……</p>

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

至于为什么……?

于 2017-03-30T21:54:26.337 回答