How do I get the currently active Twitter Account with the iOS Twitter/Accounts Framework?


#1

I currently have an objective-C class which accesses a twitter account. This is the code that verifies and gets the account:

    ACAccountStore *account = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    
    // Request access from the user to access their Twitter account
    [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
     {
         // Did user allow us access?
         if (granted == YES)
         {
             // Populate array with all available Twitter accounts
             NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
             
             // Sanity check
             if ([arrayOfAccounts count] > 0)
             {
                 ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

Calling accountsWithAccountType on ACAccountStore returns an array of ACAccount objects with the type ACAccountTypeIdentifierTwitter.

At the end of the code sample I simply use the first ACAccount in the array. However, if someone has multiple twitter accounts on their device then this code will only ever return the first ACAccount in the array. This will not work if the user has switched to the second ACAccount in their twitter app.

So is it possible to get the ACAccount that has been switched to within the twitter app? Otherwise the twitter integration in my app is useless without explicitly asking the user which account they want to connect with. My app has to be compatible with iOS 5 so I cannot use the new Social framework I don’t think.

Thanks in advance.


#2

en français si possible