在iOS 6中获取真正的twitter用户名

我想find应用程序用户的叽叽喳喳手柄的用户名,所以使用这个代码:

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]; 

然后得到每个返回的ACAccounts的用户名属性。 我发现,如果你改变你的twitter用户名,iOS仍然可以使用你的新用户名,但是把你的旧用户名保存在它的logging中,所以返回用户名属性实际上会返回你的旧用户名,然后你不能显示或者用来匹配反对任何事情

您可以通过执行以下操作获取用户标识:

 NSDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary: [twitterAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]]]; NSString *tempUserID = [[tempDict objectForKey:@"properties"] objectForKey:@"user_id"]; 

我想知道如何使用该用户名来找出他们的实际用户名?

在这里你去:twitterAccout.username应该返回实际的用户名…没有testing,但我相信几乎可以肯定,它会工作!

 ACAccountStore *accountStore = [[ACAccountStore alloc] init]; ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { if(granted) { NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; if ([accountsArray count] > 0) { ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; NSLog(@"%@",twitterAccount.username); NSLog(@"%@",twitterAccount.accountType); } } }]; 

获取用户名的Twitter用户名实际上是比应该更辛苦。 Facebook SDK使得类似的过程变得容易很多(从来没有想过我会这么说)。

无论如何,要制作Twitter信息请求,您需要将您select的帐户从“帐户”商店附加到TWRequest:

 NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"]; NSMutableDictionary *params = [NSMutableDictionary new]; [params setObject:tempUserID forKey:@"user_id"]; [params setObject:@"0" forKey:@"include_rts"]; // don't include retweets [params setObject:@"1" forKey:@"trim_user"]; // trim the user information [params setObject:@"1" forKey:@"count"]; // i don't even know what this does but it does something useful TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET]; // Attach an account to the request [request setAccount:twitterAccount]; // this can be any Twitter account obtained from the Account store [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if (responseData) { NSDictionary *twitterData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL]; NSLog(@"received Twitter data: %@", twitterData); // to do something useful with this data: NSString *screen_name = [twitterData objectForKey:@"screen_name"]; // the screen name you were after dispatch_async(dispatch_get_main_queue(), ^{ // update your UI in here twitterScreenNameLabel.text = screen_name; }); // A handy bonus tip: twitter display picture NSString *profileImageUrl = [twitterData objectForKey:@"profile_image_url"]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:profileImageUrl]]; UIImage *image = [UIImage imageWithData:imageData]; // the matching profile image dispatch_async(dispatch_get_main_queue(), ^{ // assign it to an imageview in your UI here twitterProfileImageView.image = image; }); }); }else{ NSLog(@"Error while downloading Twitter user data: %@", error); } }]; 

注意我如何用asynchronous块封装接口更新的东西。 这是为了确保界面不会冻结。 我也扔了一个奖金提示检索用户的个人资料图像,你是如此的倾向。

托马斯Verbeek的答案是正确的,但不赞成ios 7。

代替:

 TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET]; 

使用:

 SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:params]; 

另外请确保您导入了Social.framework以便能够使用它。

使用帐户框架获取Twitter帐户信息( 用户名,全名等

 - (void)getTwitterAccountInformation { ACAccountStore *accountStore = [[ACAccountStore alloc] init]; ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { if(granted) { NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; if ([accountsArray count] > 0) { ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; NSLog(@"%@",twitterAccount.username); NSDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary: [twitterAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]]]; NSString *name = [[tempDict objectForKey:@"properties"] objectForKey:@"fullName"]; } } }]; }