从 iOS 中的 Twitter 框架获取 Twitter 句柄 [英] Getting Twitter handle from Twitter Framework in iOS

查看:140
本文介绍了从 iOS 中的 Twitter 框架获取 Twitter 句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道要在 iOS 5 中使用 twitter 框架访问已配置的 twitter 帐户,可以执行以下操作:

I know that to get access to a configured twitter account using twitter framework in iOS 5, the following can be done:

ACAccountStore *t_account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
    if (granted == YES) {
    }
}

但问题是我不需要完整的帐户,如果他在 Twitter 帐户上配置了任何名称,我只想要 Twitter 名称.那么,有没有一种方法可以从框架(不是完整帐户)仅获取 twitter 句柄无需任何用户许可 ?

But the problem is that I don't have need for a full account, I just want the twitter name if he has configured any on the twitter accounts. So, Is there a way to get just the twitter handle from the framework (not the full account) without asking any user permission ?

推荐答案

未经用户授予访问这些帐户的权限,您将无法访问任何帐户.只需在新项目的应用程序委托中尝试以下操作,您就知道该应用程序没有被授予访问 twitter 帐户的权限.当然,请确保您在设备或模拟器中至少有一个 twitter 帐户,并且在您的项目和应用程序委托中导入了帐户框架.此外,您还假设用户只有一个 Twitter 帐户.最好为用户可能拥有多个帐户的情况编写代码.

You won't get access to any accounts without the user granting permission to access those accounts. Just try through the following in an app delegate in a new project so you know that app has not been given permissions to access the twitter accounts. Of course make sure you have at least one twitter account on the device or in the simulator and the accounts framework imported in your project and app delegate. Also you are making the assumption that the user has only one twitter account. Best to code for a case where a user may have more than one account.

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

    for (ACAccount *account in accountsArray ) {
        NSLog(@"Account name: %@", account.username);
    }

    NSLog(@"Accounts array: %d", accountsArray.count);

现在更改代码以在用户授予权限时返回您的结果:

Now change the code to return your results when a user has granted permission:

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {

    if(granted) {

        NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

        for (ACAccount *account in accountsArray ) {
            NSLog(@"Account name: %@", account.username);
        }

        NSLog(@"Accounts array: %d", accountsArray.count);
    }
}];

所以我想简短的回答是,苹果要求用户授予用户帐户权限是有原因的.如果未授予该访问权限,您将不会取回任何数据.

So I guess the short answer is, there is a reason apple ask the user to grant permission to user accounts. If that access has not been granted you aren't going to get any data back.

这篇关于从 iOS 中的 Twitter 框架获取 Twitter 句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆