Error creating twitter ACAccount on iOS5: NSURLErr

2019-02-20 17:43发布

问题:

I am having trouble creating and saving a new twitter account to ACAccountStore on iOS5.

After performing all the steps to init the account, ACAccountStore's saveAccount:withCompletionHandler: returns NSURLErrorDomain error -1012.

Does anyone have similar issues?

My code sample below is using requestToken to init ACAccountCrendential. This object is an OAToken (ShareKit object) initialized with the token and secret received from twitter after completing OAuth.

    ACAccountStore *store = [[[ACAccountStore alloc] init] autorelease];
    ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    ACAccount *account = [[[ACAccount alloc] initWithAccountType:twitterAccountType] autorelease];

    account.username = @"twitterusername";
    account.credential = [[[ACAccountCredential alloc] initWithOAuthToken:requestToken.key tokenSecret:requestToken.secret] autorelease]; 
    [store requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if (granted) {
            [store saveAccount:account withCompletionHandler:^(BOOL success, NSError *error) {
                if (success) {
                    NSLog(@"TWITTER ACCOUNT CREATED SUCCESSFULLY!");
                }
                else{
                    NSLog(@"ERROR creating twitter account: %@", [error description]);
                }
            }];
        }
    }]; 

The strange thing is that Apple's Accounts Framework Reference suggests that saveAccount:withCompletionHandler: attempts to perform OAuth itself:

If the account type supports authentication and the account is not authenticated, the account is authenticated using its credentials. If the authentication is successful, the account is saved; otherwise, it is not saved.

I find that very strange since there is no way for the user to authenticate directly with the server by inputting username/password.

I also tried to initialize ACAccountCredential with my application's consumer key and consumer secret, with the same result (failed with NSURLErrorDomain error -1012.)

Any guidance on this topic would be greatly appreciated!

thanks.

回答1:

I'm having the same trouble adding a Twitter account with ACAccount. However, I was able to create a Twitter account with ACAccount successfully using an authorized OAuth Access token and Access token secret, not the Consumer key and Consumer secret. (dev.twitter.com/apps->Twitter->OAuth->OAuth Setting-->Access token and Access token secret) This means that you still have to use an OAuth library to get an authorized Access token and Access token secret when the user is adding a Twitter account.

Update: You can see how Twitter recommends that you migrate existing accounts to iOS5 system accounts here. That link provided me with the best example of how to add an ACAccount.