19

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 but today I installed Xcode 7 beta 5 and when I run the app on iOS 9 Simulator, I get a dialog box titled "MailCompositionService quit unexpectedly" and when I view the error report, I see:

Application Specific Information: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7fd314280b10'

terminating with uncaught exception of type NSException abort() called CoreSimulator 179 - Device: iPhone 6 - Runtime: iOS 9.0 (13A4325c) - DeviceType: iPhone 6

The error happens when the mail composition view comes up. It freezes for a few seconds and then the error dialog box comes up.

The code that opens the mail composition view is:

if ([MFMailComposeViewController canSendMail])
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Comment title"];
    [picker setMessageBody:@"Comment description" isHTML:NO];
                                        
    [self.window.rootViewController presentModalViewController:picker animated:YES];
    [picker release];
}

If it helps to know, before the app crashes, mailComposeController:didFinishWithResult:error: is called with result = MFMailComposeResultCancelled and error = nil.

I'd appreciate tips on how to find the cause of this error. Thanks!

Community
  • 1
  • 1
Sleiman
  • 1,620
  • 1
  • 12
  • 16
  • 3
    Test on a real (development) device where you know that mail works properly. – rmaddy Aug 18 '15 at 16:47
  • Thanks! I will test on a dev device and report back. – Sleiman Aug 18 '15 at 17:07
  • @rmaddy, I did test on a real device and it works. Thanks for the tip! Please put your answer in a post so I tag it as the correct one. – Sleiman Aug 19 '15 at 09:27
  • I would suppose that's a bug on iOS 9, possibly because mail has not been set up. I can't even find a setting on the iOS 9 simulator where you can set up an email account. IIRC this also crashed on iOS 6 or 7 or so... – auco Sep 23 '15 at 14:04
  • 2
    As of XCode 7.2/iOS 9.2 simulator, this is still happening. Per https://forums.developer.apple.com/thread/4415 it looks like a known bug. Of course the [opacity of Apple's bug reporting system](http://stackoverflow.com/questions/144873/can-i-browse-other-peoples-apple-bug-reports) means we have no idea if it's gotten any love or not. Hard to believe this is still an issue! – Lane Rettig Jan 27 '16 at 15:48

5 Answers5

14

The issue is with simulator, on the real device mail composer is working correctly.

Borzh
  • 5,069
  • 2
  • 48
  • 64
  • Do you know if this would have a negative effect when publishing? I read somewhere that Apple might reject your update/app because they run it on simulator and thus the app would not function correctly, but I don't know whether this is true. – Edwin Lambregts Oct 29 '15 at 08:26
  • 1
    I just can tell you that I have apps published on Apple Store that have this issue on simulator. In my experience Apple never rejected my apps because of this issue. – Borzh Oct 29 '15 at 11:14
  • @EdwinLambregts Apple doesn't review the apps using the Simulator - this is impossible anyway as the binary you submit has not been built for i386 / x86_64 – BytesGuy Feb 18 '16 at 12:00
5

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.

Ashvin
  • 8,227
  • 3
  • 36
  • 53
1

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);

Idali
  • 1,023
  • 7
  • 10
0

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.

dev_jac
  • 825
  • 9
  • 17
0

As a simple work around for this problem, you can use "mailto" protocol, it will:

  • Not crash the app (device and simulator)
  • Prompt the user to login if the device has not login with any mail account

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)
Yuchen
  • 30,852
  • 26
  • 164
  • 234