Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling a 401 response with sendAsynchronousRequest:queue:completionHandler:

I'm making an async url request with the new iOS 5 method: sendAsynchronousRequest:queue:completionHandler:. This uses a block to handle the response, but no NSURLConnectionDelegate delegate methods are being called? I can't see a way to set the delegate for the NSUrlConnection class in this instance?

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

   NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

   NSLog(@"HTTP response code: %i", httpResponse.statusCode);

   if (error != nil) {
       NSLog(@"There was error with the synchronous request: %@", error.description);              
   }
}];

The reason I need the delegate methods is because one of my calls gets a 401 response, which iOS handles in a different way - deferring authentication to delegate methods. When a 401 response comes back - httpResponse.statusCode is just "0".

Is there a way to stop iOS from trying to handle a 401 differently? In particular, I'm thinking I need to use the continueWithoutCredentialForAuthenticationChallenge: delegate method - but no delegates are being called in this case.

Thanks!

like image 243
wows Avatar asked Nov 14 '22 07:11

wows


1 Answers

You need to use a NSURLConnection instance and use a delegate for that. The sendAsynchronousRequest:queue:completionHandler: is a shortcut for the same but with the default behavior without a delegate.

like image 62
Igor Avatar answered Feb 01 '23 22:02

Igor