Paging Facebook Graph Results

Facebook iOS SDK provides `FBSDKGraphRequest` to get friends, posts, etc. But the results are paged: you only get the first 25 friends (or 5 posts). To get the rest you need to send a new request, not provided by `FBSDKCoreKit`:

[graphRequest startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
	// first handle error and result...
	// then get the next page of results:
	NSString *nextPage = json[@"paging"][@"next"];
	if (nextPage) {
		[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:nextPage] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
			// parse data, handle results, and get the next page recursively.
		}] resume];
	}
}];

To simplify this, I wrote a simple extension to `FBSDKGraphRequest` that handles paging: FBSDKGraphRequest+Paging. Feel free to use it.

Usage:

FBSDKGraphRequest *friendsRequest = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/friends" parameters:nil];
[friendsRequest startPagingWithCompletionHandler:^(id result, NSError *error) {
    // check for error...
    // use parsed result:
    NSArray *friends = result[@"data"];
}];

Leave a Reply