MFMailComposer使用Swift在iOS 8上崩溃

时间:2014-12-03 00:55:25

标签: ios swift ios8 mfmailcomposer

以下是指向同一问题的简单应用的链接:https://drive.google.com/folderview?id=0B1t60Ehs8m62Wjl3QXl1YjY4TmM&usp=sharing

以下是我服务器上的zip文件:http://xbsjason.com/test/DemoMessageUI.zip

由于Dropbox要求我输入某些电子邮件地址进行查看/编辑,因此必须在Google云端硬盘上发帖。

我遇到这段发送邮件的问题。当用户在大约10秒钟后按下邮件按钮(表单填写或未填写)时,应用程序崩溃。在模拟器中,我可以看到它与此代码有关,但我不确定问题是什么,因为我在另一个应用程序中运行几乎相同的代码而没有任何问题。谁能在这里发现错误?

仅供参考我查看了相关问题但似乎没有解决我的错误/崩溃问题。感谢。

/* MAIL Sharing ====================================================*/
@IBAction func mailButt(sender: AnyObject) {
    var subjectText = "Glass Quote Request"
    var name = nameInput.text
    var phone = phoneNumber.text
    var year = carYear.text
    var make = carMake.text
    var model = carModel.text
    var glass = glassNeeded.text
    var messageText = "Please process quote for glass:<p>Respond to <b>\(name)</b> at     <b>\(phone)</b>. <p>The following glass is required: <p>Glass type: <b>\(glass)</b><p>For the following car:<p><b>\(year) \(make) \(model)</b> "

    var mailComposer: MFMailComposeViewController = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self
    mailComposer.setSubject(subjectText)
    mailComposer.setMessageBody(messageText, isHTML: true)
    mailComposer.setToRecipients(["validemail@gmail.com"])

    self.presentViewController(mailComposer, animated: true, completion: nil)
}
// Email results ================
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
    switch result.value {
    case MFMailComposeResultCancelled.value:
        NSLog("Mail cancelled")
    case MFMailComposeResultSaved.value:
        NSLog("Mail saved")
    case MFMailComposeResultSent.value:
        NSLog("Mail sent")
    case MFMailComposeResultFailed.value:
        NSLog("Mail sent failure: %@", [error.localizedDescription])
    default:
        break
    }
    self.dismissViewControllerAnimated(false, completion: nil)
}

2 个答案:

答案 0 :(得分:2)

var mailComposer:MFMailComposeViewController是你的问题。你需要对它进行强有力的引用,直到它完全结束 - 使用一个ivar来保存它。

答案 1 :(得分:2)

在运行该代码之前尝试检查+ canSendMail。如果您尚未在设备上设置邮件帐户,则您的应用会崩溃。

看起来更深入一点,看起来MFMailComposeViewController也存在类似于David H所说的具体问题。见I have REAL misunderstanding with MFMailComposeViewController in Swift (iOS8) in Simulator