I have a mailHelper class like following:
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)
}
}
Then I call this helper function in my main view controller:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.section == 0 && indexPath.row == 0) {
let mailHelperClass = MailHelper()
mailHelperClass.configuredInquiryMailComposeViewController(self)
}
}
When I click the cell in tableview in main view controller, Mail Composer is successfully shown. The problem is that whenever I click "Send" or "Cancel" button in the mail composer, it crashes the app.
To make sure whether this helper class is wrong, I got rid of the helper class and migrated all mail composer related functions into my main view controller. Then, it worked whenever I clicked "Send" or "Cancel" button. What am I doing wrong in my mail helper class
?
When it crashes, it does not give me any error message.