2

嗨,我正在使用MFMessageComposeViewControlleriPhone 应用程序中的消息传递。由于这是一个 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 时,我需要一种方法来禁用此按钮。

4

6 回答 6

3

您可以使用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];
   }
}

参考 :

可以发送文本

返回一个布尔值,指示当前设备是否能够发送文本消息。 + (BOOL)canSendText

返回值

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

在尝试呈现消息撰写视图控制器之前,请始终调用此方法。如果设备不支持消息传递或当前未配置为发送消息,则设备可能无法发送消息。此方法仅适用于通过 iMessage、SMS 和 MMS 发送短信的能力。

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

Available in iOS 4.0 and later.

在 MFMessageComposeViewController.h 中声明

于 2013-12-19T11:58:24.690 回答
3

MFMEssageComposeViewController 上有一个方法:

if ([MFMessageComposeViewController canSendText]) {

}

else {
    NSLog(@"Cannot send text");
}
于 2013-12-19T11:59:15.230 回答
1
    NSString *deviceType = [UIDevice currentDevice].model;
    if ([deviceType hasPrefix:@"iPod"])
    {
        //It's iPod;
        //Disable button
    }
于 2013-12-19T12:03:18.167 回答
1

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

在此处形成您可以了解有关不同设备检测的更多信息: 使用 iPhone SDK 确定设备(iPhone、iPod Touch)

- (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];
      }
   }
}
于 2013-12-19T12:21:44.670 回答
0

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

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType hasPrefix:@"iPod"])
    // it's an iPod

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

于 2013-12-19T11:58:35.460 回答
0
 -(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];
}
于 2013-12-19T12:59:47.453 回答