1

我检查是否有使用此代码的 Twitter 帐户:

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

NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountType];

twitter如果我使用帐户在 iOS 6 模拟器上运行它,arrayOfAccounts.count则为 1,但在 iPhone 4 iOS6 上,arrayOfAccounts.count则为 0。

我究竟做错了什么?

4

1 回答 1

8

我也遇到了同样的问题。使用此代码。

原因是它没有从 Twitter 帐户访问设置中获得权限,所以我们需要先检查一下。

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;
                }
            }
}
于 2012-12-10T08:47:24.060 回答