Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we use saveAccount:(ACAccount *)account of ACAccountStore class?

Please clarify me when do we use this method ? - (void)saveAccount:(ACAccount *)account withCompletionHandler:(ACAccountStoreSaveCompletionHandler)completionHandler

As I know we get access to the account through OAuth and we do not get user's credentials. So how do we create an account? I've found that ACAccount has the only one creation method: - (id)initWithAccountType:(ACAccountType *)type What actually happens when we create account this way? And can we save it now ?

like image 526
Stas Avatar asked Nov 21 '12 11:11

Stas


1 Answers

Ok, finally I've found the information about it. Consider this scenario: Our app have already been authorized by the user and we've got both access token and secret. Now we want to support new iOS 6 features and create twitter (for example) account in Settings. To do this we need to migrate these tokens to the central account store. Here's how:

- (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
        }
    }];
}

For more information about it read here how to migrate tokens

like image 152
Stas Avatar answered Nov 16 '22 10:11

Stas