I am creating
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
but picker is nil yet and application is crashing with error Terminating app due to uncaught exception 'NSInvalidArgumentException'
, reason: 'Application tried to present a nil modal view controller on target
. It is working fine in simulator but crashing in Device .
How can is use MFMailComposerViewController
with IOS 7.
You should check if MFMailComposeViewController is able to send your mail just before trying to send it (for example user could not have any mail account on the iOS device).
So in your case for Objective-C:
MFMailComposeViewController *myMailCompose = [[MFMailComposeViewController alloc] init];
if ([MFMailComposeViewController canSendMail]) {
myMailCompose.mailComposeDelegate = self;
[myMailCompose setSubject:@"Subject"];
[myMailCompose setMessageBody:@"message" isHTML:NO];
[self presentViewController:myMailCompose animated:YES completion:nil];
} else {
// unable to send mail, notify your users somehow
}
Swift 3:
let myMailCompose = MFMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
myMailCompose.mailComposeDelegate = self
myMailCompose.setSubject("Subject")
myMailCompose.setMessageBody("message", isHTML: false)
self.present(myMailCompose, animated: true, completion: nil)
} else {
// unable to send mail, notify your users somehow
}
Though checking if one can send mail helps avoiding the application crashing, it would be nice to find out, why it's actually happening. (in my case, the same app crashes on iPhone 4s but not in iPhone 5)
UPDATE: I've found following (if the reason of crashing is interesting for You!): MFMailComposeViewController produces a nil object As I used the gmail app, I didn't activated the mail support by apple. After reading this I've activated it and ... ta-da ... everything works fine!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With