So, I'm trying to use the built-in Twitter API in iOS 5 to get a list of all the followers for a given user. In all the documentation for examples I can find, API requests are passed in by built-in blocks that will be executed when the request returns, which is great for most simple things, BUT, when I try to get ~ 1000 followers, and the request returns them in the size of ~ 100, I was fixated on how to recursively call the request again using the “next swap address” returned and processed inside the completion block. Here is the code:
- (void)getTwitterFollowers {
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType =
[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[store requestAccessToAccountsWithType:twitterAccountType
withCompletionHandler:^(BOOL granted, NSError *error) {
if (!granted) {
NSLog(@"User rejected access to his account.");
}
else {
NSArray *twitterAccounts =
[store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) {
ACAccount *account = [twitterAccounts objectAtIndex:0];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"1" forKey:@"include_entities"];
NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/followers.json"];
request = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodGET];
[params release];
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!responseData) {
FullLog(@"%@", error);
}
else {
NSError *jsonError;
followers = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableLeaves
error:&jsonError];
if (followers != nil) {
FullLog(@"%@", followers);
}
else {
FullLog(@"%@", jsonError);
}
}
}];
}
}
}];
[store release];
}
- , , , . , " ", !