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

App*_*ple 1 iphone objective-c ios

我正在使用推特和iOS 5帐户框架.问题是我无法使用http://api.twitter.com/1/friends/ids.json?screen_name=%@ "这个api 获取朋友列表但是从twitter api explorer,我得到了朋友列表.(twitter explorer api = https://dev.twitter.com/console).

swi*_*Boy 5

我正在使用Twitter Native Framework for iOS.

要从Twitter获取朋友列表,您可以采用这种方式(四步).

  1. 将Twitter和Accounts Framework添加到项目中.
  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;
Run Code Online (Sandbox Code Playgroud)

你的.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);


    }];        

}
Run Code Online (Sandbox Code Playgroud)

如果您对此有任何疑问,请告诉我!乐于帮助!