iosapitwitterslrequest

How to get followers and following with Twitter/Social framework in iOS


How can I get all followers and following from a user's Twitter account.

I get followers and following counts. However I need usernames of followers and following users.

Here my code.

- (void) getAccountInfo
{
    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];

                // Creating a request to get the info about a user on Twitter

                SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"] parameters:[NSDictionary dictionaryWithObject:twitterAccount.username forKey:@"screen_name"]];
                [twitterInfoRequest setAccount:twitterAccount];

                // Making the request

                [twitterInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    dispatch_async(dispatch_get_main_queue(), ^{

                        // Check if we reached the reate 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;
                            NSArray *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];


                            // Filter the preferred data

                            NSString *screen_name = [(NSDictionary *)TWData objectForKey:@"screen_name"];
                            NSString *name = [(NSDictionary *)TWData objectForKey:@"name"];

                            int followers = [[(NSDictionary *)TWData objectForKey:@"followers_count"] integerValue];
                            int following = [[(NSDictionary *)TWData objectForKey:@"friends_count"] integerValue];


                        }
                    });
                }];
            }
        } else {
            NSLog(@"No access granted");
        }
    }];

}

Solution

  • Getting list of followers

    To get list of followers, change the URL you are requesting to:

    https://api.twitter.com/1.1/followers/list.json
    

    It returns a cursored collection of user objects for users following the specified user.

    Getting list of followed users

    To get list of the users you follow, change the URL you are requesting to:

    https://api.twitter.com/1.1/friends/list.json
    

    It returns a cursored collection of user objects for every user the specified user is following (otherwise known as their "friends").

    I used your code to test it and it works. Based on REST API v1.1 Resources