Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios: Application tried to present a nil modal view controller on target

I am developing an application,the requirement is to open email composer on a button click of UIAlertView.

message which is in message body of email is copied from UITextView. i am using following code snipt:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)
  {
      // opening message composer
  }
else
  {
   MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Test mail"];
    [picker setMessageBody:messageBody.text isHTML:YES];
    [self presentViewController:picker animated:YES completion:NULL];
  }
}
 // mail compose delegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
      [self dismissViewControllerAnimated:YES completion:NULL];
}

but the issue is that i am getting error saying Application tried to present a nil modal view controller on target. how we can open default mail composer in ios 7?

like image 754
Bhagyashree mahajan Avatar asked Oct 08 '13 04:10

Bhagyashree mahajan


3 Answers

As per Apple, You should check is MFMailComposeViewController are able to send your mail just before sending

if ([MFMailComposeViewController canSendMail]) {
     MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Test mail"];
    [picker setMessageBody:messageBody.text isHTML:YES];
    [self presentViewController:picker animated:YES completion:NULL];
}

Swift:

if MFMailComposeViewController.canSendMail() else {
// Send mail code
}

Ref : Apple Dev url


like image 159
Toseef Khilji Avatar answered Nov 24 '22 01:11

Toseef Khilji


Swift 4 version

guard MFMailComposeViewController.canSendMail() else {
    print("Mail services are not available")
    return
}
sendEmail()
like image 31
rockdaswift Avatar answered Nov 24 '22 02:11

rockdaswift


Forgetting Mail account configuration in device settings may also lead to this error. Re check whether a mail account is configured in your device or not.

like image 21
prodeveloper Avatar answered Nov 24 '22 01:11

prodeveloper