I've a class that subclass NSObject with a function do display a MFMailComposeViewController. Here is the code :
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
mailController.mailComposeDelegate = self;
[mailController setSubject:@"Sample Subject"];
[mailController setMessageBody:@"Here is some main text in the email!" isHTML:NO];
[mailController setToRecipients:@[self.email]];
UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
UINavigationController *navigationController = tabbarController.selectedViewController;
[navigationController.topViewController presentViewController:mailController animated:YES completion:NULL];
Everything works well with this code. The problem is when I want to dismiss the MFMailComposeViewController
. Sometime I get a crash, sometimes it just don't happens nothing. I've implemented the delegate function :
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
UINavigationController *navigationController = tabbarController.selectedViewController;
[navigationController.topViewController dismissViewControllerAnimated:YES completion:nil];
}
After that I tried to show and dismiss it directlty from a ViewController and everything was working. Even the cancel button.
I don't know why it works in my ViewController Class but not in my subclass of NSObject.
When I get the crash I've seen in the logs :
-[MFMailComposeInternalViewController _notifyCompositionDidFinish]
Try this,
Hope that helps.