在iOS中从Twitter框架获取Twitter处理 [英] Getting Twitter handle from Twitter Framework in iOS

查看:156
本文介绍了在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句柄

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天全站免登陆