2011-02-13 49 views
29

我设置一个MFMailComposeViewController和它的作品就好在iPhone上,但在iPad上它崩溃,他说:MFMailComposeViewController - iPad的

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

那么,为什么这个创建一个无模式的看法?

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]; 

任何想法?

回答

73

MFMailComposeViewController这样的Loos未被创建出于某种原因,因此具有nil值。在呈现之前检查它是否为零(尽管此解决方法不会回答此处出现的问题......)。

你也应该进行检查,如果邮件作曲家可以尝试创建和使用​​方法目前它之前发送邮件(如果没有邮件帐户设置在设备将返回NO为例):

if ([MFMailComposeViewController canSendMail]){ 
    // Create and show composer 
} 
else{ 
    // Show some error message here 
} 
+0

但是为什么不创建它?我打电话给alloc/init。 – RyanJM 2011-02-14 02:38:38

+19

也未必可获得例如创建,如果在设备上没有邮件帐户设置 – Vladimir 2011-02-14 08:56:34

11

你必须把检查canSendMail,你MFMailComposeViewController.h类创建MFMailComposerViewController对象之前,请参阅以下意见:

/*! 
@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); 

你的对象将不进行初始化,直到你的设备是设置发送邮件。

2

发生这种情况的原因是您的iOS默认邮件应用程序没有配置任何邮件ID。因此请使用您的任何邮件ID进行配置并尝试。

这样

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]; 
} 

希望它的帮助你。

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]; 
} 

如果您收到警报,请在手机上配置您的邮件帐户。

-3

没有在您的测试设备上设置邮件帐户。

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]; 
}