Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift NSURLSession and authentication

I am currently trying to change my code from using NSURLConnection to NSURLSession. One thing that is confusing me is authentication.

My service that I am trying to connect is basic authenticated.

In my former code I had the following method by implementing protocol NSURLConnectionDataDelegate:

func connection(connection:NSURLConnection!, willSendRequestForAuthenticationChallenge challenge:NSURLAuthenticationChallenge!) {
   if challenge.previousFailureCount > 1 {

   } else {
      let creds = NSURLCredential(user: usernameTextField.text, password: passwordTextField.text, persistence: NSURLCredentialPersistence.None)
      challenge.sender.useCredential(creds, forAuthenticationChallenge: challenge)
   }
}

Now I am stuck.

  • Do I have to implement NSURLSessionDelegate.didReceiveChallenge?
  • If so, how do I handle with the completion handler?
  • In Apple Developer Reference I have found the following line under didReceiveChallenge

    If you do not implement this method, the session calls its delegate’s URLSession:task:didReceiveChallenge:completionHandler: method instead.

  • What does this mean?

like image 810
Oliver Koehler Avatar asked Jul 08 '14 10:07

Oliver Koehler


1 Answers

Yes,

If you do not implement NSURLSessionDelegate.didReceiveChallenge method, the session calls its delegate’s URLSession:task:didReceiveChallenge:completionHandler: method instead.

Better to implement both

func URLSession(session: NSURLSession!, didReceiveChallenge challenge: NSURLAuthenticationChallenge!, completionHandler: ((NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void)!) {

    if challenge.protectionSpace.authenticationMethod.compare(NSURLAuthenticationMethodServerTrust) == 0 {
        if challenge.protectionSpace.host.compare("HOST_NAME") == 0 {
            completionHandler(.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust))
        }

    } else if challenge.protectionSpace.authenticationMethod.compare(NSURLAuthenticationMethodHTTPBasic) == 0 {
        if challenge.previousFailureCount > 0 {
            println("Alert Please check the credential")
            completionHandler(NSURLSessionAuthChallengeDisposition.CancelAuthenticationChallenge, nil)
        } else {
            var credential = NSURLCredential(user:"username", password:"password", persistence: .ForSession)
            completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential)
        }
    }

}

func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didReceiveChallenge challenge: NSURLAuthenticationChallenge!, completionHandler: ((NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void)!){

    println("task-didReceiveChallenge")

    if challenge.previousFailureCount > 0 {
        println("Alert Please check the credential")
        completionHandler(NSURLSessionAuthChallengeDisposition.CancelAuthenticationChallenge, nil)
    } else {
        var credential = NSURLCredential(user:"username", password:"password", persistence: .ForSession)
        completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential)
    }


}
like image 70
Loganathan Avatar answered Oct 31 '22 22:10

Loganathan