4

我知道要在 iOS 5 中使用 twitter 框架访问已配置的 twitter 帐户,可以执行以下操作:

ACAccountStore *t_account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
    if (granted == YES) {
    }
}

但问题是我不需要完整的帐户,如果他在 Twitter 帐户上配置了任何名称,我只想要 Twitter 名称。那么,有没有一种方法可以在不询问任何用户许可的情况下从框架(不是完整帐户)中获取Twitter 句柄

4

1 回答 1

1

未经用户授予访问这些帐户的权限,您将无法访问任何帐户。只需在新项目的应用程序委托中尝试以下操作,您就知道该应用程序没有被授予访问 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);
    }
}];

所以我想简短的回答是,苹果要求用户授予用户帐户权限是有原因的。如果未授予该访问权限,您将不会取回任何数据。

于 2012-09-05T07:32:34.400 回答