2013-07-31 34 views
0

如何使用TWRequest兩個iOS 5的用於獲取Twitter的用戶詳細信息,如何使用的iOS TWRequest爲iOS 5讓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]; 

但最近,微博關閉了API版本1和實施1.1版本,和目前上述邏輯是不是在iOS 5中的工作,因API棄用可能......

我使用SLRequest爲iOS 6中正在完善,但我想知道我們該怎麼做在iOS 5中

回答

3

等Twitter的用戶詳細信息試試這個:

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]; 
          }        
         }); 
        }]; 
       }); 
      } 
     }]; 
    } 
} 
+1

謝謝Maulik!但有時它在這個地方崩潰,可能是線程問題.. [request setAccount:[twitterAccounts lastObject]];我們能解決嗎? – Ganesh

+0

什麼是您的崩潰日誌? – Maulik

+0

我也得到同樣的問題在iOS 5,它給EXC_BAD_ACCESS, –

相關問題