如何处理多个SLRequest以获取Twitter中的好友列表?

时间:2013-04-09 06:52:20

标签: objective-c multithreading ios6 twitter

我使用以下twitter api获取好友列表: https://api.twitter.com/1.1/friends/list.json

使用[SLRequest performRequestWithHandler:^(NSData * responseData,NSHTTPURLResponse * urlResponse,NSError * error)获取响应数据,并在performRequestWithHandler块中执行UI更改和模型数据更改。

但是最多一个请求只检索了20个朋友。(如果你将api中的cursor参数设置为-1)。

我可以使用api的cursor参数发送请求以获取接下来的20个朋友,依此类推,直到游标值为0。 cursor参数可以设置为' next_cursor'上一个请求的响应数据中的参数。

但是我不知道如何在前一个请求的performRequestWithHandler中调用另一个SLRequest,直到' next_cursor'前一个请求的响应数据中的值为0.

任何人都可以告诉我如何使用SLRequest或使用任何其他方式吸引所有朋友。

感谢任何帮助。

谢谢。

1 个答案:

答案 0 :(得分:1)

你可以在得到twitter朋友的回复后立即调用请求处理程序中的下一个请求。

很抱歉没有详细说明。我以为你会明白的。 这是代码。

    ACAccountStore *account = [[ACAccountStore alloc] init];
        ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

        // Request access from the user to access their Twitter account
        [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
         {
             // Did user allow us access?
             if (granted == YES)
             {
                 // Populate array with all available Twitter accounts
                 NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

                 // Sanity check
                 if ([arrayOfAccounts count] > 0)
                 {

                     [self postRequest];

                 }
             }
         }];

- (void)PostRequest
{
       // Keep it simple, use the first account available
                     ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

                     NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
                     [tempDict setValue:@"Posting video" forKey:@"status"];

                     // Build a twitter request

                     SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"] parameters:tempDict];

                     [postRequest setAccount:acct];

                     [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                         NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                         NSString *output = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
                         NSLog(@"%@", output);
**// calling this again and again will help you with multiple post request.**
[self postRequest]
                     }];
}

也可以为朋友列表做类似的事情。

希望我帮助过。