我有一个简单的 Twitter 客户端,用于显示用户的推文、关注者和关注者。
由于某种原因,用户推文的计数参数被忽略了,它总是只加载 20 个结果。
这是代码:
- (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];
}
});
}];
}
这将始终只返回 20 个结果,即使我将计数设置为 1 或 2 之类的低值。我在定义参数时是否做错了什么?
另外,我正在尝试加载用户关注者和关注者,但我想加载总共 200 个(如果每个),但同样只加载 20 个。
从 twitter API 读取的内容来看,它提供自动分页,并使用 cursor 参数我可以迭代以加载我想要的所有数据。
我很难弄清楚这是如何工作的。这是我的跟随代码(跟随者是相同的,除了它调用不同的 API 字符串)
- (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"];
}
});
}];
}
我不确定如何正确循环,因为 twitter api 返回我需要转到下一个数据的光标值。
任何帮助都会很棒,我可能只是错过了一些我无法完全理解的逻辑。
提前致谢!