31

I've setup a MFMailComposeViewController and it works just fine on the iPhone, but on the iPad it crashes, saying:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target...

So why would this create a nil modal view?

    MFMailComposeViewController *message = [[MFMailComposeViewController alloc] init];
    [message setMessageBody:@"My message here"  isHTML:NO];
    [message setToRecipients:[NSArray arrayWithObject:@"my@domain.com"]];
    [message setSubject:@"Request Info"];
    message.mailComposeDelegate = self;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        message.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentModalViewController:message animated:YES];
    [message release];

Any ideas?

RyanJM
  • 6,728
  • 8
  • 55
  • 89

5 Answers5

73

Loos like MFMailComposeViewController was not created for some reason and thus has nil value. Check if it is nil before presenting it (although this workaround does not answer what went wrong here...).

You should also perform the check if mail composer can send mail before trying to create and present it using +canSendMail method (it will return NO for example if no mail account set up on device):

 if ([MFMailComposeViewController canSendMail]){
    // Create and show composer
 }
 else{
   // Show some error message here
 }
Vladimir
  • 167,138
  • 35
  • 380
  • 310
11

You must have to put check canSendMail, before you create the MFMailComposerViewController object, see the following comments from MFMailComposeViewController.h class:

/*!
@method     canSendMail
@abstract   Returns <tt>YES</tt> if the user has set up the device for sending email.
@discussion The client may continue to set the recipients and content if the return value was <tt>YES</tt>.  If <tt>NO</tt>
            was the result, the client has a couple options.  It may choose to simply notify the user of the inability to
            send mail, or it may issue a "mailto" URL via <tt>-[UIApplication openURL:]</tt>.
*/

+ (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

Your object won't be initialized until your device is setup for sending mails.

Adrian
  • 14,925
  • 16
  • 92
  • 163
Ansari
  • 1,827
  • 2
  • 22
  • 34
  • 1
    Thank you. That would do it. This device is with work and therefore doesn't have my email setup on it. Nice catch! – RyanJM Feb 18 '11 at 16:07
4
if ([MFMailComposeViewController   canSendMail]){
    //execute  your    code 
else{
    UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];     
    [anAlert addButtonWithTitle:@"OK"];
    [anAlert show];
}

If you are getting an alert, configure your mail account on your phone.

SmallChess
  • 7,103
  • 9
  • 49
  • 79
anand madhav
  • 353
  • 4
  • 10
2

It's happen b'cause of your iOS default mail app did't configured yet with any mail id. so configure with any of your mail id and try.

like this

if ([MFMailComposeViewController canSendMail]){
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setToRecipients:[NSArray arrayWithObject:eMail]];
    [self presentViewController:controller animated:YES completion:nil];
}
else{
    UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [anAlert addButtonWithTitle:@"Cancel"];
    [anAlert show];
}

hope its help you.

Gurumoorthy Arumugam
  • 2,091
  • 1
  • 24
  • 38
-3

No mail account set up on your testing device.

if ([MFMailComposeViewController canSendMail]){

//execute your code else{
UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [anAlert addButtonWithTitle:@"Cancel"];
    [anAlert show];
}
dmullings
  • 6,570
  • 5
  • 26
  • 28