title: Basic Authentication failures with iOS 8
body: <p>I have an iOS app that interacts with a server running Rails, and has been stable and reliable for 3 years.  The server requires Basic and SSL certification, and this has been working perfectly fine right up to and including iOS 7.</p>
  
  <p>However, I am now seeing authentication issues with devices running iOS 8.  Devices / simulators running &lt; iOS 8 continue to work fine.</p>
  
  <p>On app initialization, there is a flurry of data requests to sync with the server that need to pass Basic authentication.</p>
  
  <p>These result in the following delegate method being called,</p>
  
  <pre><code>willSendRequestForAuthenticationChallenge
  </code></pre>
  
  <p>...and the problem occurs because these are being challenged endlessly - the code deliberately fails on the second try when [challenge previousFailureCount] > 0 (the code path follows standard practice by calling cancelAuthenticationChallenge if the previousFailureCount > 0) - see below.</p>
  
  <p>I've logged the challenge ids, and these are different across each challenge, even when the previousFailureCount > 0.</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  {
  
      if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic])
      {   
          if ([challenge previousFailureCount] == 0)
          {
              NSURLCredential *newCredential;
  
              newCredential = [NSURLCredential credentialWithUser:MY_USERNAME
                              password:MY_PASSWORD
                              persistence:NSURLCredentialPersistenceForSession]; // Retain for rest of session
  
              [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
          }
          else
          {
              [[challenge sender] cancelAuthenticationChallenge:challenge];
  
              // ...error will be handled by connection didFailWithError
          }
      }
  }
  </code></pre>
  
  <p>If I knock out the call to check the previousFailureCount, there are endless invocations of the challenge method.</p>
  
  <p>However, once this flurry of failures has failed, the subsequent and later 'individual' NSUrlRequests are successfully authenticated.</p>
  
  <p>Again, this problem is specific to iOS 8.  Any ideas why a 'fast' succession of authentication requests would fail in iOS 8, but work in iOS 7?</p>
  
  <p>Thanks.</p>