Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFMailComposeViewController - iPad

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:@"[email protected]"]];
    [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?

like image 524
RyanJM Avatar asked Feb 13 '11 23:02

RyanJM


4 Answers

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
 }
like image 123
Vladimir Avatar answered Oct 20 '22 07:10

Vladimir


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.

like image 28
Ansari Avatar answered Oct 20 '22 09:10

Ansari


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.

like image 4
3 revs, 3 users 62% Avatar answered Oct 20 '22 08:10

3 revs, 3 users 62%


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.

like image 2
Gurumoorthy Arumugam Avatar answered Oct 20 '22 09:10

Gurumoorthy Arumugam