ios - 如何在 iOS 中使用 Twitter/社交框架获取关注者和关注者

标签 ios api twitter slrequest

如何从用户的 Twitter 帐户中获取所有关注者关注

我得到关注者和关注计数。但是我需要关注者和关注用户的用户名。

这是我的代码。

- (void) getAccountInfo
{
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error){
        if (granted) {

            NSArray *accounts = [accountStore accountsWithAccountType:accountType];

            // Check if the users has setup at least one Twitter account

            if (accounts.count > 0)
            {
                ACAccount *twitterAccount = [accounts objectAtIndex:0];

                // Creating a request to get the info about a user on Twitter

                SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"] parameters:[NSDictionary dictionaryWithObject:twitterAccount.username forKey:@"screen_name"]];
                [twitterInfoRequest setAccount:twitterAccount];

                // Making the request

                [twitterInfoRequest 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;
                            NSArray *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];


                            // Filter the preferred data

                            NSString *screen_name = [(NSDictionary *)TWData objectForKey:@"screen_name"];
                            NSString *name = [(NSDictionary *)TWData objectForKey:@"name"];

                            int followers = [[(NSDictionary *)TWData objectForKey:@"followers_count"] integerValue];
                            int following = [[(NSDictionary *)TWData objectForKey:@"friends_count"] integerValue];


                        }
                    });
                }];
            }
        } else {
            NSLog(@"No access granted");
        }
    }];

}

最佳答案

获取关注者列表

要获取关注者列表,请将您请求的URL更改为:

https://api.twitter.com/1.1/followers/list.json

它为关注指定用户的用户返回用户对象的游标集合。

获取关注用户列表

要获取您关注的用户列表,请将您请求的URL更改为:

https://api.twitter.com/1.1/friends/list.json

它为指定用户关注的每个用户(也称为他们的“ friend ”)返回用户对象的游标集合。

我用你的代码来测试它并且它有效。基于REST API v1.1 Resources

关于ios - 如何在 iOS 中使用 Twitter/社交框架获取关注者和关注者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23409101/

相关文章:

html - 推特是 sleep 问题

ios - iOS应用程序的证章编号是否有限制?

ios - 管理 View Controller 以维护状态

java - 使用 Swagger-UI @ApiParam 注释 @FormParam 字段

PHP 和 API 调用性能和最佳实践的一对多关系

c# - 将 OAuth 2 与桌面 c# 应用程序一起使用

ios - Twitter api 文本字段值被截断

java - twitter4j:401 身份验证凭据丢失或不正确

android - build.phonegap.com 是否有任何开源替代品?

ios - 我无法在 XCode 中删除 Outlet