Twitter登录兼容ios5.0,5.1,6.0

时间:2013-01-21 11:18:18

标签: ios5 twitter ios6 ios5.1

在ios5.0中添加了twitter框架,我正在使用它的类TWTweetComposeViewController

if([TWTweetComposeViewController canSendTweet])
    {
        NSLog(@"can send tweet");
    }
    else{
         NSString *message = @"The application cannot send a tweet at the moment. This is because it cannot reach Twitter or you don't have a Twitter account associated with this device.";
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops" message:message delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alertView show];
    }

在这一步,我需要向用户显示twitter的登录页面。如果我应该将用户重定向到设置以登录用户,我很困惑?

如果是,则此代码无效

     NSURL *twitterURL = [NSURL URLWithString:@"prefs:root=TWITTER"];
    [[UIApplication sharedApplication] openURL:twitterURL];

此外,我已经阅读了一些在ios6中不起作用的地方。那么我该怎么办才能让用户登录片刻而不是直接发送推文。现在我只想让用户登录。

我也提到this question它正在使用ios6,但在这种情况下我如何处理ios5.1?

任何帮助将不胜感激....

1 个答案:

答案 0 :(得分:2)

我用下面的条件编码来解决它。 我只有这样。如果有人有任何其他方式,请建议我。

if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
        if(SYSTEM_VERSION_EQUAL_TO(@"5.0"))
        {
            NSURL *twitterURL = [NSURL URLWithString:@"prefs:root=TWITTER"];
            [[UIApplication sharedApplication] openURL:twitterURL];
        }else{
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No_Tw", nil) message:NSLocalizedString(@"No_TW_Ac", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Button_Ok", nil) otherButtonTitles:nil, nil];
            [alertView show];
        }
    }

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
        SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        tweetSheet.view.hidden=TRUE;

        [self presentViewController:tweetSheet animated:NO completion:^{
            [tweetSheet.view endEditing:YES];
        }];
    }

如果iOS是5.0,那么它将重定向到Twitter的登录,否则它将向用户显示警告以进入设置并进行登录。对于iOS 6.0及更大版本SLComposeViewController工作正常。