未经用户授予访问这些帐户的权限,您将无法访问任何帐户。只需在新项目的应用程序委托中尝试以下操作,您就知道该应用程序没有被授予访问 twitter 帐户的权限。当然,请确保您在设备或模拟器中至少有一个 twitter 帐户,并且在您的项目和应用程序委托中导入了帐户框架。此外,您还假设用户只有一个 Twitter 帐户。最好为用户可能拥有多个帐户的情况编写代码。
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);
现在更改代码以在用户授予权限时返回您的结果:
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);
}
}];
所以我想简短的回答是,苹果要求用户授予用户帐户权限是有原因的。如果未授予该访问权限,您将不会取回任何数据。