iOS returns that there are no twitter accounts on

2019-04-02 11:23发布

I check if there is used twitter account with this code:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountType];

If i run it on iOS 6 simulator with twitter account, arrayOfAccounts.count is 1, but on iPhone 4 iOS6, arrayOfAccounts.count is 0.

What am i doing wrong?

1条回答
▲ chillily
2楼-- · 2019-04-02 12:04

I also got same problem. Use this code.

Reason for this is that it does not get permissions from settings for Twitter account access, so we need to check that first.

if ([TWRequest class])
            {
                ACAccountStore *account = [[ACAccountStore alloc] init];
                ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
                                              ACAccountTypeIdentifierTwitter];

                [account requestAccessToAccountsWithType:accountType options:nil
                                              completion:^(BOOL granted, NSError *error)
                 {
                     if (granted == YES)
                     {
                         // Get account and communicate with Twitter API
                         NSArray *arrayOfAccounts = [account
                                                     accountsWithAccountType:accountType];

                         if ([arrayOfAccounts count] > 0)
                         {
                             ACAccount *twitterAccount = [arrayOfAccounts lastObject];
                         }
                  }
                     else
                     {
                         NSLog(@"No Twitter Access Error");
                         return;
                     }
                 }];
            }



            if ([TWRequest class])
            {
                ACAccountStore *as = [[ACAccountStore alloc] init];
                ACAccountType *accountType = [as accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
                NSArray *twitterAccounts = [as accountsWithAccountType:accountType];
                if (!twitterAccounts.count)
                {
                    NSLog(@"No Twitter Account configured");

                    return;
                }
            }
}
查看更多
登录 后发表回答