我想找到app用户的twitter句柄的用户名,所以使用了这段代码:
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获取username属性。我发现如果你改变你的推特用户名,iOS仍然可以使用你的新用户名,但是保留你的旧用户名在其记录中,所以返回用户名属性实际上会返回你的旧用户名,然后你无法显示或用来匹配反对任何事情。
您可以通过以下方式获取用户ID:
NSDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:
[twitterAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]]];
NSString *tempUserID = [[tempDict objectForKey:@"properties"] objectForKey:@"user_id"];
我想知道如何使用该用户名来查找他们的实际用户名?
答案 0 :(得分:4)
这里你去:twitterAccout.username应该返回实际的用户名...没有经过测试,但我确信它几乎可以肯定它会起作用!
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);
}
}
}];
答案 1 :(得分:3)
获取用户ID的Twitter用户名实际上比它应该更费力。 Facebook SDK使类似的过程变得更加容易(从未想过我曾经说过......)。
无论如何,要发布Twitter信息请求,您需要将您选择的帐户从帐户存储附加到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);
}
}];
请注意我如何在异步块中包装接口更新。这是为了确保界面不会冻结。我还提出了一个奖金提示,用于检索用户的个人资料图片,你是否如此倾向。
答案 2 :(得分:1)
Thomas 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才能使用它。
答案 3 :(得分:0)
使用帐户框架
获取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"];
}
}
}];
}