【问题标题】:MFMailComposeViewController - iPadMFMailComposeViewController - iPad
【发布时间】:2011-02-13 23:45:51
【问题描述】:

我设置了一个 MFMailComposeViewController,它在 iPhone 上工作得很好,但在 iPad 上它崩溃了,说:

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

那么为什么这会创建一个 nil 模态视图呢?

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

有什么想法吗?

【问题讨论】:

    标签: ipad mfmailcomposeviewcontroller


    【解决方案1】:

    MFMailComposeViewController 这样的Loos 由于某种原因没有创建,因此具有nil 值。在呈现之前检查它是否为 nil(尽管此解决方法不能回答这里出了什么问题......)。

    您还应该在尝试使用+canSendMail 方法创建和呈现邮件之前检查邮件编写器是否可以发送邮件(例如,如果设备上没有设置邮件帐户,它将返回 NO):

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

    【讨论】:

    • 但是为什么不创建呢?我正在调用 alloc/init。
    • 它可能不会被创建,例如,如果设备上没有设置邮件帐户
    • 谢谢弗拉基米尔 - 你的评论让我省去了一大堆调试工作
    • 如果设备上没有设置邮件账户,MFMailComposeViewController 将返回 nil
    • 嗨。我在我的 iPad 上设置了一封电子邮件,但 MFMailComposeViewController 仍然返回 NO。你们谁能告诉我为什么?谢谢。
    【解决方案2】:

    您必须检查canSendMail,在创建MFMailComposerViewController 对象之前,请参阅MFMailComposeViewController.h 类中的以下cmets:

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

    在您的设备设置好发送邮件之前,您的对象不会被初始化。

    【讨论】:

    • 谢谢。这样就可以了。此设备正在工作,因此没有我的电子邮件设置。不错的收获!
    【解决方案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:@"OK"];
        [anAlert show];
    }
    

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

    【讨论】:

      【解决方案4】:

      这是因为您的 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];
      }
      

      希望对您有所帮助。

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-15
          • 2011-07-17
          • 1970-01-01
          • 1970-01-01
          • 2010-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多