7

请澄清一下我们什么时候使用这种方法? - (void)saveAccount:(ACAccount *)account withCompletionHandler:(ACAccountStoreSaveCompletionHandler)completionHandler

据我所知,我们可以通过 OAuth 访问该帐户,但我们没有获得用户的凭据。那么我们如何创建一个帐户呢?我发现 ACAccount 只有一种创建方法: - (id)initWithAccountType:(ACAccountType *)type 当我们以这种方式创建帐户时,实际会发生什么?我们现在可以保存它吗?

4

1 回答 1

12

好的,我终于找到了有关它的信息。考虑这种情况:我们的应用程序已经被用户授权,并且我们已经获得了访问令牌和秘密。现在我们要支持新的 iOS 6 功能并在设置中创建 twitter(例如)帐户。为此,我们需要将这些令牌迁移到中央帐户存储。就是这样:

- (void)storeAccountWithAccessToken:(NSString *)token secret:(NSString *)secret {
    //We start creating an account by creating the credentials object
    ACAccountCredential *credential = [[ACAccountCredential alloc] initWithOAuthToken:token tokenSecret:secret];
    ACAccountType *twitterAcctType =[self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    ACAccount *newAccount = [[ACAccount alloc] initWithAccountType:twitterAcctType];
    //Here we assign credentials and now can save account
    newAccount.credential = credential;

    [self.accountStore saveAccount:newAccount withCompletionHandler:^(BOOL success, NSError *error) {

        if (success) {
            NSLog(@"the account was saved!");
        }
        else {
            //Handle error here
        }
    }];
}

有关它的更多信息,请在此处阅读如何迁移令牌

于 2012-11-26T14:19:09.717 回答