您如何在iOS 6中获取当前Twitter用户的关注者数量.TWRequest已弃用,那么如何使用新的Social.Framework来获取关注者的数量?
答案 0 :(得分:0)
首先,您需要Authenticate
您的请求(获取权限)。
第二,请参阅以下步骤:
1.下载FHSTwitterEngine
Twitter图书馆。
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
,将其添加到NSArray
,然后从{{1}获取Count
}:
NSArray
我测试了这段代码,它完全正常工作^ _ ^。
答案 1 :(得分: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]);
}
});
}
});
}