我有一个 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/