简体   繁体   中英

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:

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. So, Is there a way to get just the twitter handle from the framework (not the full account) without asking any user permission ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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