【问题标题】:On iOS how can I get the list of followers that the user is also following on Twitter?在 iOS 上,如何获取用户也在 Twitter 上关注的关注者列表?
【发布时间】:2014-08-08 12:13:57
【问题描述】:

在我的应用程序中,我想向 Twitter 朋友发送直接消息以邀请使用该应用程序。为了发送直接消息,我需要目标人关注用户。但是,我不想列出所有关注者,而只想列出用户也关注的关注者。有没有办法在 iOS 中获取此列表?

谢谢

【问题讨论】:

    标签: ios objective-c twitter twitter-oauth


    【解决方案1】:

    使用FHSTwitterEngine

    #import "FHSTwitterEngine.h"

    添加SystemConfiguration.framework

    将以下代码写入您的 viewDidLoad(用于 oauth 登录)

    UIButton *logIn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    logIn.frame = CGRectMake(100, 100, 100, 100);
    [logIn setTitle:@"Login" forState:UIControlStateNormal];
    [logIn addTarget:self action:@selector(showLoginWindow:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logIn];
    
    [[FHSTwitterEngine sharedEngine]permanentlySetConsumerKey:@"Xg3ACDprWAH8loEPjMzRg" andSecret:@"9LwYDxw1iTc6D9ebHdrYCZrJP4lJhQv5uf4ueiPHvJ0"];
    [[FHSTwitterEngine sharedEngine]setDelegate:self];
    
    
     - (void)showLoginWindow:(id)sender {
    UIViewController *loginController = [[FHSTwitterEngine sharedEngine]loginControllerWithCompletionHandler:^(BOOL success) {
        NSLog(success?@"L0L success":@"O noes!!! Loggen faylur!!!");
        [[FHSTwitterEngine sharedEngine]loadAccessToken];
        NSString *username = [FHSTwitterEngine sharedEngine].authenticatedUsername;
        NSLog(@"user name is :%@",username);
        if (username.length > 0) {
            [self listResults];
        }
    }];
         [self presentViewController:loginController animated:YES completion:nil];
    }
     - (void)listResults {
    
    NSString *username = [FHSTwitterEngine sharedEngine].authenticatedUsername;
    NSMutableDictionary *   dict1 = [[FHSTwitterEngine sharedEngine]listFriendsForUser:username isID:NO withCursor:@"-1"];
    
    //  NSLog(@"====> %@",[dict1 objectForKey:@"users"] );        // Here You get all the data
    NSMutableArray *array=[dict1 objectForKey:@"users"];
    for(int i=0;i<[array count];i++)
    {
        NSLog(@"names:%@",[[array objectAtIndex:i]objectForKey:@"name"]);
    }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个。此代码获取关注者和以下名称

       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];
      
                  for(ACAccount *t in accounts)
                  {
                      if([t.username isEqualToString:username])
                      {
                          twitterAccount = t;
                          break;
                      }
                  }
      
                  SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friends/ids.json?"] parameters:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@", username], @"screen_name", @"-1", @"cursor", nil]];
                  [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];
      
      
                          }
                      });
                  }];
              }
          } else {
              NSLog(@"No access granted");
          }
      }];
      

      【讨论】:

      • 需要更多信息使用这个stackoverflow.com/questions/11600621/…
      • 什么是用户名??以及如何获得它??
      • 你可以从这里获取用户名:twitterAccount.username
      • 如何获得20多条结果?我正在使用更新光标值指向下一页,并获取数据,但在 10、12 页之后,我得到“达到速率限制”状态代码 429,这样做的最佳方法是什么?
      • 你应该设置 count=200,其中 200 是返回的最大限制数,20 是默认值
      猜你喜欢
      • 1970-01-01
      • 2017-09-08
      • 2018-09-14
      • 2015-06-20
      • 2012-07-20
      • 1970-01-01
      • 2011-10-13
      • 2013-02-02
      • 1970-01-01
      相关资源
      最近更新 更多