1

嗨,如何在 iOS 6 中获取当前 twitter 用户的关注者数量。TWRequest 已贬值,那么如何使用新的 Social.Framework 获取关注者数量?

4

2 回答 2

0

使用此代码

(IBAction)listFriends:(id)sender {
    NSMutableArray *arr = [[NSMutableArray alloc]init];
dict=[[NSMutableDictionary alloc]init];
[_tweetField resignFirstResponder];
dispatch_async(GCDBackgroundThread, ^{
    @autoreleasepool {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        //to get friends id.......
        NSLog(@"Friends' IDs: %@",[[FHSTwitterEngine sharedEngine]getFriendsIDs]);


       /* dict = [[FHSTwitterEngine sharedEngine]getFollowersIDs];

         for (NSDictionary *item in [dict objectForKey:@"ids"]) {
         [arr addObject:[dict objectForKey:@"ids"]];

         }*/


  // To get friends name ...

       // NSLog(@"Friends_Name: %@",[[FHSTwitterEngine sharedEngine]listFriendsForUser:_loggedInUserLabel.text isID:NO withCursor:@"-1"]);



        dict = [[FHSTwitterEngine sharedEngine]listFriendsForUser:_loggedInUserLabel.text isID:NO withCursor:@"-1"];

        NSLog(@"====> %@",[dict objectForKey:@"users"] );
        NSMutableArray *array=[dict objectForKey:@"users"];
        for(int i=0;i<[array count];i++)
        {
            NSLog(@"names:%@",[[array objectAtIndex:i]objectForKey:@"name"]);

        }
        // NSLog(@"Friends_Name: %@",[[FHSTwitterEngine sharedEngine]getMentionsTimelineWithCount:1000 sinceID:nil maxID:nil]);


        dispatch_sync(GCDMainThread, ^{
            @autoreleasepool {
                UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Complete!" message:@"Your list of followers has been fetched" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [av show];
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                // NSLog(@"====> %d",[arr count]);
            }
        });
    }
});

}

于 2013-10-21T09:36:21.267 回答
0

首先,您需要Authenticate您的请求(获得许可)。

其次,请参阅以下步骤:

1.下载FHSTwitterEngine推特库。

2.将文件夹FHSTwitterEngine“添加到您的项目和#import "FHSTwitterEngine.h".

3.添加SystemConfiguration.framework到您的项目中。

用法: 1.在[ViewDidLoad]添加以下代码:

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:@"<consumer_key>" andSecret:@"<consumer_secret>"];
    [[FHSTwitterEngine sharedEngine]setDelegate:self];

并且不要忘记导入委托 FHSTwitterEngineAccessTokenDelegate。

您需要获得您的请求的权限,使用以下方法将显示登录窗口:

- (void)showLoginWindow:(id)sender {
    [[FHSTwitterEngine sharedEngine]showOAuthLoginControllerFromViewController:self withCompletion:^(BOOL success) {
        NSLog(success?@"L0L success":@"O noes!!! Loggen faylur!!!");
    }];
}

出现登录窗口时,输入您的 Twitter 用户名和密码以验证您的请求。

将以下方法添加到您的代码中:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[FHSTwitterEngine sharedEngine]loadAccessToken];
    NSString *username = [[FHSTwitterEngine sharedEngine]loggedInUsername];// self.engine.loggedInUsername;
    if (username.length > 0) {
        lbl.text = [NSString stringWithFormat:@"Logged in as %@",username];
        [self listResults];


    } else {
        lbl.text = @"You are not logged in.";
    }

}
- (void)storeAccessToken:(NSString *)accessToken {
    [[NSUserDefaults standardUserDefaults]setObject:accessToken forKey:@"SavedAccessHTTPBody"];
}

- (NSString *)loadAccessToken {
    return [[NSUserDefaults standardUserDefaults]objectForKey:@"SavedAccessHTTPBody"];
}

4.现在您已准备好获取您的请求,以下方法将列出您的followers ID's,将它们添加到 anNSArrayCount从 中获取NSArray

- (Void)listFriends:(id)sender {
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    [_tweetField resignFirstResponder];
    dispatch_async(GCDBackgroundThread, ^{
        @autoreleasepool {
            [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

           // NSLog(@"Friends' IDs: %@",[[FHSTwitterEngine sharedEngine]getFriendsIDs]);
            dict = [[FHSTwitterEngine sharedEngine]getFollowersIDs];
            for (NSDictionary *item in [dict objectForKey:@"ids"]) {
                [arr addObject:[dict objectForKey:@"ids"]];


            }
            dispatch_sync(GCDMainThread, ^{
                @autoreleasepool {
                    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Complete!" message:@"Your list of followers has been fetched" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [av show];
                    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
 NSLog(@"====> %d",[arr count]);


                }
            });
        }
    });
}

我测试了这段代码,它运行良好^_^。

于 2013-07-01T11:25:10.773 回答