2014-07-08 18 views
7

Obecnie próbuję zmienić mój kod z wykorzystaniem NSURLConnection na NSURLSession. Jedną z rzeczy, które mnie mylą, jest uwierzytelnianie.Swift NSURLSession i uwierzytelnianie

Moja usługa, z którą próbuję się połączyć, jest uwierzytelniana w sposób podstawowy.

W moim poprzednim kodzie miałem następującą metodę poprzez wdrożenie protokołu 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) 
    } 
} 

Teraz siedzę.

  • Czy muszę wprowadzić NSURLSessionDelegate.didReceiveChallenge?
  • Jeśli tak, w jaki sposób postępować z procedurą obsługi zakończenia?
  • w Apple Developer Reference znalazłem następującą linię pod didReceiveChallenge

    Jeśli nie wdrożenie tej metody, sesja nazywa swojego delegata za URLSession: Zadanie: didReceiveChallenge: completionHandler: zamiast metody.

  • Co to oznacza?

Odpowiedz

7

Tak,

Jeśli nie wdrażać metodę NSURLSessionDelegate.didReceiveChallenge, sesja nazywa swojego delegata za URLSession: Zadanie: didReceiveChallenge: completionHandler: zamiast metody.

Lepiej realizować zarówno

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) 
    } 


} 
Powiązane problemy