简体   繁体   English

从iOS中的Twitter Framework获取Twitter句柄

[英]Getting Twitter handle from Twitter Framework in iOS

I know that to get access to a configured twitter account using twitter framework in iOS 5, the following can be done: 我知道要使用iOS 5中的twitter框架访问已配置的twitter帐户,可以完成以下操作:

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

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. 但是问题是我不需要完整的帐户,如果他在Twitter帐户上配置了任何名称,我只想要Twitter名称。 So, Is there a way to get just the twitter handle from the framework (not the full account) without asking any user permission ? 因此,有没有办法在不征求任何用户许可的情况下从框架中仅获取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. 只需在新项目的应用程序委托中尝试以下操作,即可知道该应用程序未获得访问Twitter帐户的权限。 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. 当然,请确保您在设备上或模拟器中至少有一个Twitter帐户,并且已在项目和应用程序委托中导入了帐户框架。 Also you are making the assumption that the user has only one twitter account. 另外,您还假设用户只有一个Twitter帐户。 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. 如果未授予该访问权限,则不会取回任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM