My iPhone app was rejected by TestFlight because it crashed the iPad. The offending code is trying to show a controller for sending an e-mail. I've reduced the code to a small, simple example that works as expected on my iPhone 5c running iOS 9.3.1 but crashes my iPad 2 running iOS 9.3.1:
- (void)viewDidLoad {
[super viewDidLoad];
[self displayComposerSheet];
}
-(void)displayComposerSheet {
// Create e-mail interface
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"iPad crash test"];
// Add recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"email@somewhere.com"];
[picker setToRecipients:toRecipients];
// Fill body
NSString *emailBody = @"A short test of iPad crashes";
[picker setMessageBody:emailBody isHTML:NO];
// Show interface - iPad crashes here but iPhone is ok
[self presentViewController:picker animated:YES completion:nil];
}
The error message is:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target .'
I've spent a bit of time figuring this out but haven't been able to work it out. Any ideas or suggestions would be greatly appreciated!
In order to send email there should be an email account defined on the device. If an account is not set up MFMailComposeViewController will create problem and crash.
It is better to check your device is capable of sending email via canSendMail method.
if ([MFMailComposeViewController canSendMail])
[self presentViewController:picker animated:YES completion:nil];