I am using Twitter Native Framework for iOS.
To getting friends list from Twitter you can go this way (Four Steps).
- Add Twitter and Accounts Framework to project.
- Get The Current Twitter Account Instance.
- Then you will get the Friends ID list from Twitter through API
Request.
- 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!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…