Search code examples
iosswiftmfmailcomposeviewcontrollermessageui

MFMailComposeViewController crashing on presentViewController on iOS 10.2


Well, this is very minor issue on which I have already spent couple of hours researching and trying to figure out why the app is crashing.

Let's say I've two view controllers VC1, VC2 and I'm calling MFMailComposeViewController from VC2.

So far I have tried transitioning from VC1 to VC2..

  1. via performSegueIdentifier
  2. via Storyboard ID
  3. via Storyboard ID with UINavigationController(rootViewController: vc2)

but nothing worked. I even tried embedding UINavigationViewController to the VC2 but no luck either.

Below is the IBAction method in VC2

@IBAction func sendEmail(sender: AnyObject) {
    if MFMailComposeViewController.canSendMail() {
        let mailComposerVC = configuredMailComposeViewController()
        presentViewController(mailComposerVC, animated: true, completion: nil) // CRASH
    } else {
        showSendMailErrorAlert()
    }
}


func configuredMailComposeViewController() -> MFMailComposeViewController {

    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients(["[email protected]"])
    mailComposerVC.setSubject("Reg: ")

    return mailComposerVC
}

func showSendMailErrorAlert() {
    let alert = UIAlertController(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", preferredStyle: .Alert)
    presentViewController(alert, animated: true, completion: nil)
}

All outlets and event references are also good.

Crash log

[__NSCFNumber pointSize]: unrecognized selector sent to instance 0xb0000000000000e5
2017-01-16 16:52:55.887082 Sample[2507:671461] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber pointSize]: unrecognized selector sent to instance 0xb0000000000000e5'

Solved:

The issue is with the custom navigation bar. I have reset the UINavigationBar appearance when presenting MFMailComposeViewController and setting it back on dismiss. This post helped me solve it.

I have created below two methods in a global file.

static func applyGlobalNavigationBarAppearance() {
    UINavigationBar.appearance().barTintColor = UIColor.blueColor()
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontSize()]
}

static func applyMailNavigationBarAppearance() {
    UINavigationBar.appearance().barTintColor = UIColor.whiteColor()
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = nil
}

Solution

  • Strange! My guess here is that you have set something (a font?) badly via UIAppearance and the mail composer is the first time this appearance property is being referenced. Does your project use UIAppearance (e.g. UINavigationBar.appearance)? If so, comment them out for now. See if that fixes the problem, then figure out which call is the culprit.