This article presents a useful code snippets for interacting with Twitter’s Friends & Followers REST APIs, which provide simple interfaces for most Twitter functionality.
It shows how to implement getting Twitter followers list and friends list in iOS using Social
and Accounts
frameworks.
Getting list of followers.
To get list of followers, we will contact the following endpoint.
https://api.twitter.com/1.1/followers/list.json
We will pass the screen name of the currently logged in user as a query parameter.
As a result, we will get a collection of user objects for users following the specified user.
Implementation
ACAccountStore *accountStore = [[ACAccountStore alloc] init]; ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { if (granted) { NSArray *accounts = [accountStore accountsWithAccountType:accountType]; // Check if the users has setup at least one Twitter account. if (accounts.count > 0) { ACAccount *twitterAccount = [accounts objectAtIndex:0]; NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/followers/list.json"]; NSDictionary *parameters = @{@"screen_name" : twitterAccount.username}; // Creating a request. SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:parameters]; [request setAccount:twitterAccount]; // Perform the request. [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^ { // Check if we reached the rate limit. if ([urlResponse statusCode] == 429) { NSLog(@"Rate limit reached"); return; } // Check if there was an error if (error) { NSLog(@"Error: %@", error.localizedDescription); return; } // Check if there is some response data. if (responseData) { NSError *error = nil; NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error]; NSArray *users = dictionary[@"users"]; NSLog(@"Users: %@", users); } }); }]; } else { NSLog(@"No accounts"); } } else { NSLog(@"No access granted"); } }];
Getting list of followed users (friends).
To get list of the users you follow, the only thing we have to change is the URL of the endpoint
https://api.twitter.com/1.1/friends/list.json
The endpoint returns a cursored collection of user objects for every user the specified user is following (otherwise known as their “friends”).
More information about Twitter’s REST API can be found in the REST API v1.1 Resources.
Rate limit
Twitter’s API introduces a rate limit. When an application exceeds the rate limit for a given API endpoint, the Twitter API will return an HTTP 429 “Too Many Requests” response code instead of the variety of codes you would find across the REST APIs.
The limits are quite low and they can be easily hit. You can read more about them here.
The post Getting Twitter followers list using Social framework in iOS. appeared first on stunning co.de.