0

我有一个分享 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];


}

}

这是我的代码。

4

2 回答 2

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 版本不支持 Social 框架(即低于 6.0),您基本上只是向 nil 发送消息,这是允许的。在这种情况下,您将回退到使用 Twitter 框架,每个人都可以愉快地发推文!

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

于 2013-01-08T19:19:43.493 回答
-1

要单独获取系统版本,您可以在这里找到一个很好的答案:我们如何以编程方式检测设备运行的 iOS 版本?

但是,总而言之,您可以调用:

[[[UIDevice currentDevice] systemVersion] floatValue];

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

但是,这对于您需要它是一种不好的做法。最好在检查当前操作系统的同时检查某个功能。要完全成功集成 Twitter,您应该考虑包括 iOS 5.0 的内置 Twitter 功能(您需要弱包含和 #import Twitter.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.
}
于 2013-01-08T19:19:25.957 回答