想要显示朋友列表从twitter到iPhone应用程序

我正在使用Twitter和帐户框架的iOS 5.问题是,我无法获得使用http://api.twitter.com/1/friends/ids.json?screen_name=%@ “这个API的朋友的列表。但是从twitter api资源pipe理器中,我得到了朋友列表。(twitter explorer api = https://dev.twitter.com/console )。

我正在使用iOS的Twitter Native Framework。

要从Twitter获取朋友列表,你可以这样(四步)。

  1. 将Twitter和帐户框架添加到项目。
  2. 获取当前的Twitter帐户实例。
  3. 然后,您将通过API请求从Twitter获取朋友ID列表。
  4. 最后,您可以通过ID获取好友名称或其他数据并放入数组中

所以…你的.h文件应该看起来像这样

#import <UIKit/UIKit.h> #import <Twitter/Twitter.h> #import <Accounts/Accounts.h> @interface LoginView : UIViewController{ ACAccount *myAccount; NSMutableString *paramString; NSMutableArray *resultFollowersNameList; } @property(nonatomic,retain) ACAccount *myAccount; @property(nonatomic, retain) NSMutableString *paramString; @property(nonatomic, retain) NSMutableArray *resultFollowersNameList; 

和你的.m文件应该有这样的..

 Get The Twitter Account Instance /******To check whether More then Twitter Accounts setup on device or not *****/ -(void)getTwitterAccounts { ACAccountStore *accountStore = [[ACAccountStore alloc] init]; ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { if (granted && !error) { accountsList = [accountStore accountsWithAccountType:accountType]; int NoOfAccounts = [accountsList count]; if (NoOfAccounts > 1) { NSLog(@"device has more then one twitter accounts %i",NoOfAccounts); } else { myAccount = [accountsList objectAtIndex:0]; NSLog(@"device has single twitter account : 0"); } } else { // show alert with information that the user has not granted your app access, etc. } }]; } /************* getting followers/friends ID list code start here *******/ // so far we have instnce of current account, that is myAccount // -(void) getTwitterFriendsIDListForThisAccount{ /*** url for all friends *****/ // NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/friends/ids.json"]; /*** url for Followers only ****/ NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/followers/ids.json"]; NSDictionary *p = [NSDictionary dictionaryWithObjectsAndKeys:myAccount.username, @"screen_name", nil]; TWRequest *twitterRequest = [[TWRequest alloc] initWithURL:url parameters:p requestMethod:TWRequestMethodGET]; [twitterRequest setAccount:myAccount]; [twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResposnse, NSError *error) { if (error) { } NSError *jsonError = nil; // Convert the response into a dictionary NSDictionary *twitterFriends = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError]; NSArray *IDlist = [twitterFriends objectForKey:@"ids"]; NSLog(@"response value is: %@", IDlist); int count = IDlist.count; for (int i=0; i<count; i++ ) { [paramString appendFormat:@"%@",[IDlist objectAtIndex:i]]; if (i <count-1) { NSString *delimeter = @","; [paramString appendString:delimeter]; } } NSLog(@"The mutable string is %@", paramString); [self getFollowerNameFromID:paramString]; } ]; } -(void) getFollowerNameFromID:(NSString *)ID{ NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/users/lookup.json"]; NSDictionary *p = [NSDictionary dictionaryWithObjectsAndKeys:ID, @"user_id",nil]; NSLog(@"make a request for ID %@",p); TWRequest *twitterRequest = [[TWRequest alloc] initWithURL:url parameters:p requestMethod:TWRequestMethodGET]; [twitterRequest setAccount:myAccount]; [twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if (error) { } NSError *jsonError = nil; NSDictionary *friendsdata = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError]; // NSLog(@"friendsdata value is %@", friendsdata); // resultFollowersNameList = [[NSArray alloc]init]; resultFollowersNameList = [friendsdata valueForKey:@"name"]; NSLog(@"resultNameList value is %@", resultFollowersNameList); }]; } 

让我知道如果你有任何疑问! 乐于帮助!