title: iOS/Cocoa - NSURLSession - Handling Basic HTTPS Authorization
body: <p>[edited to provide more information]</p>
  
  <p>(I'm not using AFNetworking for this project. I may do so in future, but wish to resolve this problem/misunderstanding first.)</p>
  
  <p><strong>SERVER SETUP</strong></p>
  
  <p>I cannot provide the real service here, but it is a simple, reliable service that returns XML according to a URL such as:</p>
  
  <p><a href="https://username:password@example.com/webservice">https://username:password@example.com/webservice</a></p>
  
  <p>I want to connect to the URL over HTTPS using GET, and determine any authentication failures (http status code 401).</p>
  
  <p>I have confirmed that the web service is available, and that I can successfully (http status code 200) grab XML from the url using a specified username and password. I have done this with a web browser, and with AFNetworking 2.0.3, and by using NSURLConnection.</p>
  
  <p>I have also confirmed that I am using the correct credentials at all stages.</p>
  
  <p>Given the correct credentials and the the following code:</p>
  
  <pre><code>// Note: NO delegate provided here.
  self.sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
  self.session = [NSURLSession sessionWithConfiguration:self.sessionConfig
                                    delegate:nil
                               delegateQueue:nil];
  
  NSURLSessionDataTask *dataTask = [self.session dataTaskWithURL:self.requestURL     completionHandler: ...
  </code></pre>
  
  <p><strong>The above code will work</strong>. It will successfully connect to the server, get a http status code of 200, and return the (XML) data.</p>
  
  <p><strong>PROBLEM 1</strong></p>
  
  <p>This simple approach fails in cases where the credentials are invalid. In that case, the completion block is never called, no status code (401) is provided, and eventually, the Task times out.</p>
  
  <p><strong>ATTEMPTED SOLUTION</strong></p>
  
  <p>I assigned a delegate to the NSURLSession, and am handling the following callbacks:</p>
  
  <pre><code>-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  {
      if (_sessionFailureCount == 0) {
          NSURLCredential *cred = [NSURLCredential credentialWithUser:self.userName password:self.password persistence:NSURLCredentialPersistenceNone];        
      completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
      } else {
          completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
      }
      _sessionFailureCount++;
  }
  
  - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
  didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
   completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition,    NSURLCredential *credential))completionHandler
  {
      if (_taskFailureCount == 0) {
          NSURLCredential *cred = [NSURLCredential credentialWithUser:self.userName password:self.password persistence:NSURLCredentialPersistenceNone];        
          completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
      } else {
          completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
      }
      _taskFailureCount++;
  }
  </code></pre>
  
  <p><strong>PROBLEM 1 WHEN USING ATTEMPTED SOLUTION</strong></p>
  
  <p>Please note the use of ivars _sessionFailureCount and _taskFailureCount. I am using these because the challenge object's @previousFailureCount property is never advanced! It always remains at zero, no matter how many times these callback methods are called.</p>
  
  <p><strong>PROBLEM 2 WHEN USING ATTEMPTED SOLUTION</strong></p>
  
  <p>Despite the use of correct credentials (as proven by their successful use with a nil delegate), authentication is failing. </p>
  
  <p>The following callbacks occur:</p>
  
  <pre><code>URLSession:didReceiveChallenge:completionHandler:
  (challenge @ previousFailureCount reports as zero)
  (_sessionFailureCount reports as zero)
  (completion handler is called with correct credentials)
  (there is no challenge @error provided)
  (there is no challenge @failureResponse provided)
  
  
  URLSession:didReceiveChallenge:completionHandler:
  (challenge @ previousFailureCount reports as **zero**!!)
  (_sessionFailureCount reports as one)
  (completion handler is called with request to cancel challenge)
  (there is no challenge @error provided)
  (there is no challenge @failureResponse provided)
  
  // Finally, the Data Task's completion handler is then called on us.
  (the http status code is reported as zero)
  (the NSError is reported as NSURLErrorDomain Code=-999 "cancelled")
  </code></pre>
  
  <p>(The NSError also provides a NSErrorFailingURLKey, which shows me that the URL and credentials are correct.)</p>
  
  <p>Any suggestions welcome!</p>