title: Authentication Challenge Method Is Not Called When Using NSURLSession Custom Delegate
body: <p>I'm working on an iOS app which connects to an ASP.NET Web API through Restful services. I want to use a custom delegate to handle authentication challenge. But the delegate method doesn't get called. </p>
  
  <p>The http request is written in the following method within a view controller:</p>
  
  <pre><code>- (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];
  }
  </code></pre>
  
  <p>In the AuthChallengeDelegate class, I have implemented the following method:</p>
  
  <pre><code>- (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);
  }
  </code></pre>
  
  <p>The first method (didReceiveResponse) get called and the response status code is 401 with "Www-Authenticate" = Bearer in the header field. But the second method (didReceiveChallenge) is not called. Anyone here could give me an idea of why it's not called?</p>
  
  <p>(I'm using Xcode 6 and simulating in iOS8)</p>
  
  <p>Thanks.</p>