我们何时使用ACAccountStore类的saveAccount:(ACAccount*)帐户?

Sta*_*tas 7 iphone facebook objective-c ios

请告诉我什么时候使用这种方法? - (void)saveAccount:(ACAccount *)account withCompletionHandler:(ACAccountStoreSaveCompletionHandler)completionHandler

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

Sta*_*tas 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
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

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