ios - 按下邮件编辑器中的发送按钮后应用程序崩溃

标签 ios swift email

我有一个 mailHelper 类,如下所示:

class MailHelper: NSObject, MFMailComposeViewControllerDelegate {
    //MARK: Mail Function
    func configuredInquiryMailComposeViewController(rootViewController: UIViewController) {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "AvenirNext-Medium", size: 20)!]
        mailComposerVC.navigationBar.tintColor = UIColor.whiteColor()
        mailComposerVC.navigationBar.translucent = false
        mailComposerVC.mailComposeDelegate = self
        mailComposerVC.setToRecipients(["(email)"])
        mailComposerVC.setSubject("(title)")
        mailComposerVC.setMessageBody("[Please write your inquiries below. We will reply shortly]", isHTML: false)

        if MFMailComposeViewController.canSendMail() {
            rootViewController.presentViewController(mailComposerVC, animated: true, completion: nil)
        } else {
                            showSendMailErrorAlert()
        }
    }

    func showSendMailErrorAlert() {
        let sendMailErrorAlert = UIAlertController(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", preferredStyle: UIAlertControllerStyle.Alert)
        let cancelAction: UIAlertAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
        sendMailErrorAlert.addAction(cancelAction)
        self.presentViewController(sendMailErrorAlert, animated: true, completion: nil)
    }

    // MARK: MFMailComposeViewControllerDelegate Method
    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }

}

然后我在主视图 Controller 中调用这个辅助函数:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if (indexPath.section == 0 && indexPath.row == 0) {
                let mailHelperClass = MailHelper()
               mailHelperClass.configuredInquiryMailComposeViewController(self)
            }
}

当我单击主视图 Controller 中表格 View 中的单元格时,邮件编辑器成功显示。问题是,每当我单击邮件编辑器中的“发送”或“取消”按钮时,应用程序都会崩溃。

为了确定这个帮助程序类是否错误,我删除了该帮助程序类并将所有与邮件编辑器相关的功能迁移到我的主视图 Controller 中。然后,每当我单击“发送”或“取消”按钮时,它就会起作用。我在我的邮件助手类中做错了什么?

当它崩溃时,它不会给我任何错误消息。

最佳答案

您的问题并不明显,但这是 MFMailComposeViewControllerDelegate 的问题。当您使用自定义类呈现邮件编辑器时,您已将该类的对象分配为委托(delegate),并且未保留该对象。一旦 MailHelper 的对象离开它的范围,它就会被释放,当您尝试在邮件编辑器上发送或取消时,它会向已释放的对象发送消息,因为该对象不再存在于您的应用程序中崩溃了,就是这样!

另外,在创建邮件编辑器的对象之前,您应该检查邮件编辑器是否canSendMail(),如果它无法发送邮件,为什么要创建该对象?

希望这有帮助!

关于ios - 按下邮件编辑器中的发送按钮后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36053710/

相关文章:

iOS CLLocationManager : Removing spikes

javascript - 如何在打开之前从 react-native 打开 Gmail 并传递照片

ios - 自定义UIview自由旋转太慢

ios - 如何在事件发生后的给定约束内返回 SKSpriteNode?

ios - 每周询问用户费率

swift - 如何解包 UInt32?在 swift ?

php - 使用 SwiftMailer 发送的邮件不使用样式 block

python - Django 发送邮件

ios - 如何在 Swift 中为 URL 编码大型数据对象?

ios - ZXing 在 iPod 上运行(崩溃...)