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?
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
}
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With