想要将Twitter上的好友列表显示到iPhone应用程序中 [英] want to display Friends list from twitter into iPhone app

查看:107
本文介绍了想要将Twitter上的好友列表显示到iPhone应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS 5的Twitter和Accounts Framework. 问题是我无法使用 http:获取朋友列表: //api.twitter.com/1/friends/ids.json?screen_name=%@ 这个api.但是从twitter api资源管理器中,我得到了好友列表.(twitter资源管理器api =

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

推荐答案

我正在使用iOS的Twitter Native Framework.

I am using Twitter Native Framework for iOS.

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

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

  1. 将Twitter和Accounts Framework添加到项目中.
  2. 获取当前的Twitter帐户实例.
  3. 然后您将通过API从Twitter获得好友ID列表 请求.
  4. 最后,您可以通过ID获得好友名称或其他数据, 放入数组
  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

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

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;

,并且您的.m文件应该是这样的.

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!

这篇关于想要将Twitter上的好友列表显示到iPhone应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆