如何使用iOS TWRequest for iOS 5获取Twitter用户详细信息

时间:2013-07-31 07:39:59

标签: iphone ios twitter twrequest

如何在iOS 5中使用TWRequest获取Twitter用户详细信息,

TWRequest之前工作正在以这种方式使用

 NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/users/show.json"];
 NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,@"screen_name",nil];
                                 request = [[TWRequest alloc] initWithURL:url
                                                                      parameters:params
                                                                   requestMethod:TWRequestMethodGET];

但最近,Twitter关闭了api版本1并实现了1.1版本,而现在上述逻辑在iOS 5中无效,因为API可能会被弃用......

我正在使用iOS 6的SLRequest它工作得很完美,但我想知道如何在iOS 5中获取Twitter用户详细信息

1 个答案:

答案 0 :(得分:3)

试试这个:

   NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
   NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,@"screen_name",nil];
   request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];                                                                 

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *twitterAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSArray *twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];

    // Runing on iOS 6
    if (NSClassFromString(@"SLComposeViewController") && [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        [accountStore requestAccessToAccountsWithType:twitterAccountType options:NULL completion:^(BOOL granted, NSError *error)
         {
             if (granted)
             {
                 SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url                                      parameters:parameters];

                 [request setAccount:[twitterAccounts lastObject]];

                 dispatch_async(dispatch_get_main_queue(), ^                                
                 {

                     [NSURLConnection sendAsynchronousRequest:request.preparedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)                      
                     {                         
                         dispatch_async(dispatch_get_main_queue(), ^                                        
                         {                             
                             if (data)                                 
                             {                                 
                                 [self loadData:data];                                 
                             }                             
                         });                                         
                     }];                     
                 });
             }
         }];
    }
    else if (NSClassFromString(@"TWTweetComposeViewController") && [TWTweetComposeViewController canSendTweet]) // Runing on iOS 5
    {
        [accountStore requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error)
         {
             if (granted)
             {
                 TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET];
                 [request setAccount:[twitterAccounts lastObject]];

                 dispatch_async(dispatch_get_main_queue(), ^                                
                 {                     
                     [NSURLConnection sendAsynchronousRequest:request.signedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)                      
                     {                         
                         dispatch_async(dispatch_get_main_queue(), ^                                        
                         {                             
                             if (data)                                 
                             {                                 
                                 [self loadData:data];
                             }                             
                         });
                     }];
                 });
             }
         }];
    }
}