Search code examples
iosobjective-ctwitter

iOS twitter API loadings more than 20 followers/following/tweets


I have a simple Twitter client that I am using to display a users tweets, followers, and following.

For some reason the count parameter for user tweets is being ignored and it is always loading only 20 results.

Here is the code:

- (void)getUserTweets {
    
    // 1. Create a variable for twitter
    NSURL *feedURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];
    
    // 2. Get AppDelegate reference
    AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
    
    // 3. Create NSDictionary for the TWR parameters
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: self.usernameToLoad, @"screen_name", @"true", @"include_user_entities", @"100", @"count", nil];
    
    
    // 4. Create TWRequest, with parameters, URL & specify GET method
    //TWRequest *twitterFeed = [[TWRequest alloc] initWithURL:feedURL parameters:parameters requestMethod:TWRequestMethodGET];
    SLRequest *twitterFeed = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:feedURL parameters:params];
    
    // 5. Specify twitter request's account to our app delegate's
    twitterFeed.account = appDelegate.userAccount;
    
    
    // Making the request
    
    [twitterFeed 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;
                self.userTweets = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
                                                                
            }
        });
    }];
    
}

This will always return only 20 results, even if I set the count to something low like 1 or 2. Am I doing something wrong in defining my parameters?

Also, I am trying to load the users followers and following but I want to load a total of 200 if each, but again it is only loading 20.

From what the Twitter API reads, It supplies automatic pagination and using the cursor parameter I can iterate through to load all of the data I want.

I am having a hard time figuring out exactly how this works. Here is my code for following (followers is identical with the exception of it calling a different API string)

- (void)getFriends {
    
    // 1. Create a variable for twitter
    NSURL *feedURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/friends/list.json"];
    
    // 2. Get AppDelegate reference
    AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
    
    // 3. Create NSDictionary for the TWR parameters
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: self.usernameToLoad, @"screen_name", @"true", @"include_user_entities", nil];
    
    // 4. Create TWRequest, with parameters, URL & specify GET method
    //TWRequest *twitterFeed = [[TWRequest alloc] initWithURL:feedURL parameters:parameters requestMethod:TWRequestMethodGET];
    SLRequest *twitterFeed = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:feedURL parameters:parameters];
    
    // 5. Specify twitter request's account to our app delegate's
    twitterFeed.account = appDelegate.userAccount;
    
    
    // Making the request
    
    [twitterFeed 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;
                NSDictionary *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
                
                
                
                self.userFriends = [TWData objectForKey:@"users"];
                
            }
        });
    }];
    
}

I am not sure how to properly loop through because the Twitter API returns the cursor value I need to go to the next data.

Any help would be great, I might just be missing some logic I can't quite put my finger on.


Solution

  • Results are given in multiple "pages" of results can be navigated through using the next_cursor value in subsequent requests.

    https://dev.twitter.com/docs/misc/cursoring