Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
817 views
in Technique[技术] by (71.8m points)

ios - want to display Friends list from twitter into iPhone app

I am using Twitter and Accounts Framework for iOS 5. Problem is that i am not able to get the list of friends using http://api.twitter.com/1/friends/ids.json?screen_name=%@" this api. But from twitter api explorer, i got the friends list. (twitter explorer api = https://dev.twitter.com/console ).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am using Twitter Native Framework for iOS.

To getting friends list from Twitter you can go this way (Four Steps).

  1. Add Twitter and Accounts Framework to project.
  2. Get The Current Twitter Account Instance.
  3. Then you will get the Friends ID list from Twitter through API Request.
  4. And finally you can get the Friends Name or other data via ID and put in Array

so...your .h file should look like this

#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;

and your .m file should have like this..

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);


    }];        

}

let me know if you have any doubt regarding this!! happy to help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...