I can\'t get MFMailComposeViewController to open without throwing a fatal error in iOS 9 Simulator.
The same code (Objective C) works flawlessly in iOS 8.x and lower
The issue is with simulator, on the real device mail composer is working correctly.
As per Apple Developer Forum, more details are here.
The simulator doesn't support mail. You should likely try testing mail functionality in a device.
As a simple work around for this problem, you can use "mailto" protocol, it will:
Example in swift:
Swift 3.0
let mailRecipient = "support@abc.com"
let mailSubject = "Help with ABC for iOS"
let mailBody = "xxx"
let mailTo = "mailto:\(mailRecipient)?subject=\(mailSubject)&body=\(mailBody)"
guard let escapedMailTo = mailTo.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
NSLog("Invalid mail to format")
return
}
guard let url = NSURL(string: escapedMailTo) else {
NSLog("Invalid mail to format: \(escapedMailTo)")
return
}
UIApplication.sharedApplication().openURL(url)
Swift 2.3
let mailRecipient = "support@abc.com"
let mailSubject = "Help with ABC for iOS"
let mailBody = "xxx"
let mailTo = "mailto:\(mailRecipient)?subject=\(mailSubject)&body=\(mailBody)"
guard let escapedMailTo = mailTo.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) else {
NSLog("Invalid mail to format")
return
}
guard let url = NSURL(string: escapedMailTo) else {
NSLog("Invalid mail to format: \(escapedMailTo)")
return
}
UIApplication.sharedApplication().openURL(url)
I have no idea why it happens or how did I discovered it, the crash seems to be generated by setting the NSFontAttributeName in the Appearance proxy for the navigation bar, if I uncomment that line the app crashes.
NSDictionary* format = @{
NSForegroundColorAttributeName:[UIColor whiteColor],
//NSFontAttributeName: [UIFont boldSystemFontOfSize:20],
};
[[UINavigationBar appearance] setTitleTextAttributes:format];
Please @Sleiman try and see if this fixes the issue for you too.
You should use :
[self.window.rootViewController presentViewController:picker animated:YES completion:NULL];
presentModalViewController is DEPRECATED since ios6 is and has been replaced by presentViewController:animated:completion:
ie:
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated NS_DEPRECATED_IOS(2_0, 6_0);