I am working on an iOS application that connects to ASP.NET web API through Restful services. I want to use a custom delegate to perform authentication. But the delegate method is not called.
An HTTP request is written as follows in the view controller:
- (IBAction)test:(UIButton *)sender { //Get Bearer Token KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"BearerToken" accessGroup:nil]; NSString *bearerToken = [keychainItem objectForKey:(__bridge id)(kSecValueData)]; //Configure request NSURL *url = [NSURL URLWithString:@"......"]; //Replace the .... with real IP Address NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"GET"]; [request setValue:[NSString stringWithFormat:@"Bearer %@", bearerToken] forHTTPHeaderField:@"Authorization"]; //Configure session NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AuthChallengeDelegate *authChallengeDel = [[AuthChallengeDelegate alloc] init]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:authChallengeDel delegateQueue:nil]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request]; [task resume]; }
In the AuthChallengeDelegate class, I performed the following method:
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler { NSLog(@"%@", response); } - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { NSLog(@"did receive challenge method called"); NSLog(@"%@", challenge.protectionSpace.authenticationMethod); }
The first method (didReceiveResponse) is called, and the response status code is 401 with "Www-Authenticate" = Bearer in the header field. But the second method (didReceiveChallenge) is not called. Can anyone here give me an idea of ββwhy it is not called?
(I use Xcode 6 and model in iOS8)
Thanks.
source share