iOS twitter API加载超过20个关注者/关注/推文

时间:2013-07-13 20:37:05

标签: ios api twitter

我有一个简单的Twitter客户端,用于显示用户的推文,关注者和关注者。

由于某种原因,用户推文的count参数被忽略,它总是只加载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];

            }
        });
    }];

}

即使我将计数设置为1或2之类的低值,这也总是只返回20个结果。我在定义参数时做错了吗?

此外,我正在尝试加载用户关注者和关注者,但我想加载总计200,如果每个,但再次只加载20。

从twitter API读取,它提供自动分页和使用游标参数,我可以迭代加载我想要的所有数据。

我很难弄清楚它是如何工作的。这是我的跟随代码(跟随者除了调用不同的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返回我需要转到下一个数据的光标值。

任何帮助都会很棒,我可能会错过一些我无法理解的逻辑。

提前致谢!

2 个答案:

答案 0 :(得分:0)

结果在多个“页面”中给出,可以通过在后续请求中使用next_cursor值来导航结果。

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

答案 1 :(得分:0)

很好,我在分页推文中遇到了同样的问题,它从来没有用来更新“max_id”参数,因为它的值是“NSNumber”,然后我改为NSString然后它对我来说非常好,Cross再次检查天气在请求中使用除NSString之外的任何对象