【问题标题】:ios MFMessageComposeViewController ipod crashios MFMessageComposeViewController ipod 崩溃
【发布时间】:2013-12-19 11:55:24
【问题描述】:

您好,我正在使用MFMessageComposeViewController 在 iPhone 应用程序中发送消息。 由于这是一个 iPhone 应用程序,它也支持 iPod。当点击消息按钮时,应用程序崩溃,因为 iPod 上没有消息。那么有没有办法检查设备是否是 iPod,这样我就可以隐藏消息按钮,这样用户就不会点击 iPod 中的消息并崩溃。

这是我用于消息传递的代码。

- (IBAction)Message:(id)sender
{
MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
messaging.messageComposeDelegate=self;
[messaging setBody:@"Will of the People""\n""http://bit.ly/1gZhZye"];

[self presentViewController:messaging animated:YES completion:nil];
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:^{UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Done" message:nil delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [alert show];
}];
}

这似乎在 iPhone 上运行良好。当用户使用 iPod 时,我需要一种方法来禁用此按钮。

【问题讨论】:

    标签: ios iphone ipod mfmessagecomposeview


    【解决方案1】:

    您可以使用canSendText 类方法来执行此操作:

    - (IBAction)Message:(id)sender
    {
       if ([MFMessageComposeViewController  canSendText])
       {
          MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
          messaging.messageComposeDelegate=self;
          [messaging setBody:@"Will of the People""\n""http://bit.ly/1gZhZye"];
          [self presentViewController:messaging animated:YES completion:nil];
       }
    }
    

    参考:

    canSendText

    返回一个布尔值,指示当前设备是否为 能够发送短信。 + (BOOL)canSendText

    返回值

    如果设备可以发送短信,则为“是”,如果不能,则为“否”。 讨论

    在尝试显示消息之前始终调用此方法 组成视图控制器。设备可能无法发送消息,如果它 不支持消息传递或当前未配置为 发送信息。此方法仅适用于发送文本的能力 通过 iMessage、SMS 和 MMS 发送消息。

    收到发送文本可用性更改的通知 消息,注册为观察者 MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification 通知。可用性

    Available in iOS 4.0 and later.
    

    在 MFMessageComposeViewController.h 中声明

    【讨论】:

    【解决方案2】:

    MFMEssageComposeViewController 上有一个方法:

    if ([MFMessageComposeViewController canSendText]) {
    
    }
    
    else {
        NSLog(@"Cannot send text");
    }
    

    【讨论】:

      【解决方案3】:
          NSString *deviceType = [UIDevice currentDevice].model;
          if ([deviceType hasPrefix:@"iPod"])
          {
              //It's iPod;
              //Disable button
          }
      

      【讨论】:

        【解决方案4】:

        首先使用以下方式检测设备,如果设备不支持信使则显示警报。

        您可以在此处获取有关不同设备检测的更多信息: Determine device (iPhone, iPod Touch) with iPhone SDK

        - (IBAction)Message:(id)sender
        {
           NSString *deviceType = [UIDevice currentDevice].model;
        
            if([deviceType isEqualToString:@"iPod Touch 5G"])  {
        
                 // here your alert view to show msg 
        
        
            } else {
        
                if ([MFMessageComposeViewController  canSendText])
                {
                  MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
                  messaging.messageComposeDelegate=self;
                 [messaging setBody:@"Will of the People""\n""http://bit.ly/1gZhZye"];
                 [self presentViewController:messaging animated:YES completion:nil];
              }
           }
        }
        

        【讨论】:

          【解决方案5】:

          您可以使用UIDevice 类来检查设备类型

          NSString *deviceType = [UIDevice currentDevice].model;
          
          if([deviceType hasPrefix:@"iPod"])
              // it's an iPod
          

          或者您可以使用[MFMessageComposeViewController canSendText] 来检查消息是否可以从设备发送

          【讨论】:

          • @CW0007007 能否请您仔细阅读问题?它说他想禁用 iPod 设备的按钮。所以我回答了这个问题。我知道这不是正确的方法。
          • 嗯,不,他想为 iPod 禁用它,因为它由于 iPod 无法发送文本而崩溃(这是他的特定用例)。我的方法是针对所有可能不支持发短信的设备的包罗万象的方法。而您的方法仅限于 iPod,如果以后添加多设备支持,将难以维护。
          • @PgmFreek 可能你应该像其他人一样建议使用 canSendText ,如果他需要禁用按钮而不是尝试像这样的其他解决方案..?
          【解决方案6】:
           -(IBAction)btnByEmailPressed:(id)sender
           {
               if ([MFMailComposeViewController canSendMail] == NO)
           {
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"This device is not able to send mail or Email account is not configured." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
                 [alert show];
                 return;
             }
             else
             {
          
                 MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
                 controller.mailComposeDelegate = self;
                 [controller setTitle:@"Invitation"];
                 [controller setSubject:@"My Subject"];
                 [controller setMessageBody:@"Your Text" isHTML:YES];
                 [self presentViewController:controller animated:YES completion:nil];
             }
          }
          
           - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
          {
          [self becomeFirstResponder];
          NSString *strMailResult;
          switch (result)
          {
              case MFMailComposeResultCancelled:
                  strMailResult = NSLocalizedString(@"E-Mail Cancelled",@"");
                  break;
              case MFMailComposeResultSaved:
                  strMailResult = NSLocalizedString(@"E-Mail Saved",@"");
                  break;
              case MFMailComposeResultSent:
                  strMailResult = NSLocalizedString(@"E-Mail Sent",@"");
                  break;
              case MFMailComposeResultFailed:
                  strMailResult = NSLocalizedString(@"E-Mail Failed",@"");
                  break;
              default:
                  strMailResult = NSLocalizedString(@"E-Mail Not Sent",@"");
                  break;
          }
             UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"STEP-BY-STEP-STORY",@"") message:strMailResult delegate:self  cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
             [alertView show];
             [self dismissViewControllerAnimated:YES completion:nil];
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-12-18
            • 2012-06-09
            • 1970-01-01
            • 2012-11-28
            • 1970-01-01
            • 2011-03-08
            • 1970-01-01
            • 2011-08-26
            相关资源
            最近更新 更多