从iOS的Twitter框架中获取Twitter账号名称

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账户(而不是完整的账户)而无需请求任何用户权限
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);
    }
}];

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


是的!我知道这个。现在我认为即使我不想要完整的账户,也没有任何方法可以跳过“用户权限弹窗”。我只想获取用户在Twitter账户中配置的Twitter用户名。 - defactodeity
这就是我试图用更多的代码示例来回答的原因。除非:[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)返回true,否则您将无法访问任何Twitter帐户的任何信息。 - Michael
谢谢 Michael,我已经尝试过所有的方式了。但是我一直希望 Apple 有两种不同的账户访问模式: 1)只能读取并知道 Twitter 用户名。 2)可以获取完整的 Twitter 账户,能够读/写/发布任何数据到该账户。 - defactodeity
没问题。只获取用户句柄就好了,但如果用户没有授予任何账户信息访问权限,我可以理解为什么他们不会给我们那个。 - Michael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接