MFMailComposeViewController - iPad
Asked Answered
S

5

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:@"[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?

Suspend answered 13/2, 2011 at 23:45 Comment(0)
S
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
 }
Slesvig answered 14/2, 2011 at 0:18 Comment(5)
But why would it not be created? I'm calling alloc/init.Suspend
It may not get created for example if no mail account setup on deviceSlesvig
Thanks Vladimir - your comment just saved me a whole mess of debugJoy
MFMailComposeViewController will return nil if no mail account is set on the deviceTricrotic
Hi. I have an email set up on my iPad, yet the MFMailComposeViewController still returns NO. Can anyone of you tell me why? Thanks.Whitworth
P
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.

Prohibitory answered 18/2, 2011 at 13:7 Comment(1)
Thank you. That would do it. This device is with work and therefore doesn't have my email setup on it. Nice catch!Suspend
B
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.

Brunson answered 13/2, 2011 at 23:46 Comment(0)
L
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.

Lemuel answered 16/4, 2013 at 7:27 Comment(0)
C
-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];
}
Cavalcade answered 16/12, 2014 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.