如果您使用的是iOS 5.1,我希望此按钮显示错误消息

时间:2013-01-08 19:09:58

标签: iphone ios ipad

我有一个共享Twitter消息的按钮。问题是社交网络在iOS 5.1上不起作用所以我的问题是如果用户使用iOS 5.1,我该如何发送错误消息?

-(IBAction)Twitter:(id)sender{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {

    SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

    SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultCancelled) {

            NSLog(@"Cancelled");

        } else

        {
            NSLog(@"Done");
        }

        [controller dismissViewControllerAnimated:YES completion:Nil];
    };
    controller.completionHandler =myBlock;

    [controller setInitialText:@"#VOX"];
    [controller addURL:[NSURL URLWithString:@""]];
    [controller addImage:[UIImage imageNamed:@""]];

    [self presentViewController:controller animated:YES completion:Nil];

}
else{
    alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please check your Twitter settings." delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil ,nil];

    [alert show];


}

}

这是我的代码。

2 个答案:

答案 0 :(得分:4)

如果您支持iOS 5.1作为部署目标,则不允许用户发布他们的推文是一种糟糕的用户体验。相反,您的方法应如下所示:

- (IBAction)sendTweetTapped:(id)sender {

   if ([SLComposeViewController class]) {
      // Execute your code as you have it
   }
   else {
      // Use TWTweetComposeViewController and the Twitter framework
   }
}

您需要弱化社交框架。这样做,如果用户的iOS版本不支持社交框架(即小于6.0),那么基本上只是发送消息给nil,这是允许的。在这种情况下,你会回到使用Twitter框架,每个人都会愉快地发推文!

**注意:我更改了方法的名称,因为它很糟糕,并没有描述该方法应该做什么。

答案 1 :(得分:-1)

要获得系统版本,您可以在此处找到一个好的答案:How can we programmatically detect which iOS version is device running on?

总而言之,您可以致电:

[[[UIDevice currentDevice] systemVersion] floatValue];

将iOS版本作为浮点值返回。

然而,这是不良做法,因为它需要它。最好检查功能以及检查当前的操作系统。完全成功整合Twitter你应该考虑包括iOS 5.0内置的Twitter功能(你需要弱地包括和#import Twitter.framework和Social.framework):

float osv = [[[UIDevice currentDevice] systemVersion] floatValue];

if (osv >= 6.0 && [SLComposeViewController class]) { //Supports SLComposeViewController, this is preferable.

   if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
       //Success, you can tweet! (using the class SLComposeViewController)
   } else {
       if ([TWTweetComposeViewController canSendTweet]) { //Perhaps redundant, but worth a try maybe?
            //Success, you can tweet! (using the class TWTweetComposeViewController)
       } else {
            //Error Message 
       }
   } 

} else if (osv < 6.0 && osv >= 5.0 && [TWTweetComposeViewController class]) {

   if ([TWTweetComposeViewController canSendTweet]) {
        //Success, you can tweet! (using the class TWTweetComposeViewController)
   } else {
        //Error Message 
   }

} else {
       //No internal solution exists. You will have to go with 3rd party or write your own.
}