Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Application tried to present a nil modal view controller on target' error/crash when trying to open mail composer

Tags:

I have a simple app, which opens a modal view to send email. Am using Xcode 4.2 and iOS 5, and am testing with iOS Simulator. The app crashes with Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Application tried to present a nil modal view controller on target .'

when executing the line:

 [self presentModalViewController:mailComposer animated:YES]; 

though I have initialized the object 'mailComposer'.

Class com_FirstViewController.m :

#import "com_FirstViewController.h" ... @implementation com_FirstViewController .... .... -(void)showEmailComposer {  Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (mailClass != nil) {     if ([mailClass canSendMail]) {                     NSLog(@"showEmailComposer: Calling displayComposerSheet");         [self displayComposerSheet];      } else {                     NSLog(@"showEmailComposer: Calling launchMailAppOnDevice");         [self launchMailAppOnDevice];     } } else {             NSLog(@"showEmailComposer: Calling launchMailAppOnDevice");     [self launchMailAppOnDevice]; } }    #pragma mark - #pragma mark Compose Mail  -(void) displayComposerSheet {      mailComposer = [[MFMessageComposeViewController alloc] init];     mailComposer.messageComposeDelegate = self;      // Set the mail title     [mailComposer setTitle:@"Mail Title"];      // Set the recipients     NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];      [mailComposer setRecipients:toRecipients];      // EMail Body     NSString *mailBody = @"This is the mail body";     [mailComposer setBody:mailBody];      NSLog(@"present the modal view ctlr");     [self presentModalViewController:mailComposer animated:YES]; } ... ... 

Any pointers please?