title: Swift NSURLSession and authentication
body: <p>I am currently trying to change my code from using NSURLConnection to NSURLSession.
  One thing that is confusing me is authentication. </p>
  
  <p>My service that I am trying to connect is basic authenticated.</p>
  
  <p>In my former code I had the following method by implementing protocol NSURLConnectionDataDelegate:</p>
  
  <pre><code>func connection(connection:NSURLConnection!, willSendRequestForAuthenticationChallenge challenge:NSURLAuthenticationChallenge!) {
     if challenge.previousFailureCount &gt; 1 {
  
     } else {
        let creds = NSURLCredential(user: usernameTextField.text, password: passwordTextField.text, persistence: NSURLCredentialPersistence.None)
        challenge.sender.useCredential(creds, forAuthenticationChallenge: challenge)
     }
  }
  </code></pre>
  
  <p>Now I am stuck.</p>
  
  <ul>
  <li>Do I have to implement NSURLSessionDelegate.didReceiveChallenge?</li>
  <li>If so, how do I handle with the completion handler?</li>
  <li><p>In Apple Developer Reference I have found the following line under didReceiveChallenge</p>
  
  <blockquote>
    <p><em>If you do not implement this method, the session calls its delegate’s URLSession:task:didReceiveChallenge:completionHandler: method instead.</em></p>
  </blockquote></li>
  <li><p>What does this mean?</p></li>
  </ul>