1

我想找到应用程序用户的 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 中获取用户名属性。我发现如果你更改了你的 twitter 用户名,iOS 仍然可以使用你的新用户名,但会将你的旧用户名保留在它的记录中,所以返回 username 属性实际上会返回你的旧用户名,然后你不能显示或使用它来匹配反对任何事情。

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

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

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

4

4 回答 4

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);
        }
    }
}];
于 2013-06-17T19:48:23.980 回答
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);
    }
}];

请注意我如何将接口更新内容包装在异步块中。这是为了确保界面不会冻结。我还为检索用户的个人资料图像提供了奖励提示,您是否如此倾向于。

于 2013-09-14T23:00:01.477 回答
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 以便能够使用它。

于 2014-01-28T13:09:06.087 回答
0

使用 Accounts 框架获取 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"];
            }
        }
    }];
}
于 2015-09-02T09:25:55.693 回答