How to fix NSURLErrorDomain error -1012 in authent

2019-07-07 06:53发布

问题:

I'm dealing the authenticate issue with Tumblr account using [NSURLConnection sendAsynchronousRequest:queue:completionHandler:] to send the authenticate request, but here I meet a tough problem:

  • Whenever I send the request at the first time, everything goes perfectly, but when the first authentication is done and then resend the request second time, there comes "NSURLErrorDomain error -1012".
  • The authenticate page is loaded in a webview so that the authentication should be done in my app without a browser. But it is interesting that if the process runs in a browser there comes no error, errors only happen when using webview.
  • It was weird that the authentication goes with the same code, but only the first authentication can be done, only if I reinstall the app can I authenticate it again, and after this the problem comes again.
  • I did everything I can chase to solve the issue, I clean the cache and cookie in webview, step the authentication process to see parameters, set the cachePolicy of the request but nothing helps.
  • I also found that on ios6 the process goes without any error. But on ios7 I get the -1012.
  • code -1012 tells me that the user cancelled the authentication, but the process goes automatically and I do not cancel it.

I'm wondering if the problem comes from the NSURLConnection.

- (void)authenticate:(NSString *)URLScheme WithViewController:(UIViewController *)con callback:(TMAuthenticationCallback)callback {
self.threeLeggedOAuthTokenSecret = nil;
self.hostViewController = con;
self.callback = callback;
[self emptyCookieJar];

NSString *tokenRequestURLString = [NSString stringWithFormat:@"http://www.tumblr.com/oauth/request_token?oauth_callback=%@", TMURLEncode([NSString stringWithFormat:@"%@://tumblr-authorize", URLScheme])];
NSLog(@"%@", tokenRequestURLString);

NSMutableURLRequest *request = mutableRequestWithURLString(tokenRequestURLString);
NSLog(@"%@", request);
[[self class] signRequest:request withParameters:nil consumerKey:self.OAuthConsumerKey
           consumerSecret:self.OAuthConsumerSecret token:nil tokenSecret:nil];
[self openOAuthViewController];
NSURLConnectionCompletionHandler handler = ^(NSURLResponse *response, NSData *data, NSError *error) {
    NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;
    if (error) {
        if (callback) {
            callback(nil, nil, error);
        }
        return;
    }


    NSLog(@"%d", statusCode);
    if (statusCode == 200) {
        self.threeLeggedOAuthCallback = callback;

        NSDictionary *responseParameters = formEncodedDataToDictionary(data);
        self.threeLeggedOAuthTokenSecret = responseParameters[@"oauth_token_secret"];

        NSURL *authURL = [NSURL URLWithString:
                          [NSString stringWithFormat:@"http://www.tumblr.com/oauth/authorize?oauth_token=%@",
                           responseParameters[@"oauth_token"]]];

        [self initOAuthViewControllerWithURL:authURL];
    } else {
        if (callback) {
            callback(nil, nil, errorWithStatusCode(statusCode));
        }
    }
};
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:handler];
}

Code above, everything goes normally before [NSURLConnection sendAsynchronousRequest:queue:completionHandler:],and after this method I got the error in completionHandler.