MFMailComposeViewController视图不会第二次解除

时间:2017-03-30 21:28:41

标签: ios swift email messageui

我认为这是一个独特的问题。我无法让我的电子邮件窗口解散。我正在使用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)
}

1 个答案:

答案 0 :(得分:4)

您每次都需要创建一个新的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
    }
}

至于为什么......?