【问题标题】:How to get followers and following with Twitter/Social framework in iOS如何在 iOS 中使用 Twitter/Social 框架获得关注者和关注者
【发布时间】:2014-06-18 00:20:23
【问题描述】:

如何从用户的 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");
        }
    }];

}

【问题讨论】:

  • 此代码不起作用,因为应用程序在执行时崩溃 [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL grant, NSError *error) 行有原因“原因:'-[__NSCFConstantString accessKeys]:无法识别的选择器发送到实例“

标签: ios api twitter social


【解决方案1】:

获取关注者列表

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

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

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

获取关注用户列表

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

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

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

我使用您的代码对其进行了测试,并且可以正常工作。基于REST API v1.1 Resources

【讨论】:

    【解决方案2】:

    如果您不想使用 API,可以使用 Criexe API。 https://github.com/criexe/api/wiki/Social-Networks-Page-Stats-API

    示例:

    获取https://api.criexe.com/social/pageStats?twitter=microsoft

    {
       "twitter":{
          "user":"microsoft",
          "followers":8293150
       },
       "total":8293150
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 2011-08-23
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 2012-07-20
      • 2013-08-28
      相关资源
      最近更新 更多