Kahsn
Kahsn

Reputation: 1045

App crashes after send button in Mail Composer is pressed

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.

Upvotes: 2

Views: 1311

Answers (1)

Fahri Azimov
Fahri Azimov

Reputation: 11770

Your problem here is not obvious, but it's the problem of the MFMailComposeViewControllerDelegate. When you present mail composer using your custom class, you have assigned object of that class as a delegate, and didn't retain the object. As soon as object of MailHelper left it's scope, it got released, and when you have tried to send or cancel on mail composer, it sent message to the deallocated object, since object was not there anymore your app crashed, that's it!

Also, you should be checking if mail composer canSendMail() before creating the object of the mail composer, if it can't send mail, why do you create the object?

Hope this helps!

Upvotes: 4

Related Questions