2014-07-08 11 views
7

Attualmente sto cercando di cambiare il mio codice dall'utilizzo di NSURLConnection a NSURLSession. Una cosa che mi confonde è l'autenticazione.Swift NSURLSessione e autenticazione

Il servizio che sto cercando di connettersi è autenticato di base.

Nel mio primo codice che ho avuto il seguente metodo mediante l'attuazione del protocollo 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) 
    } 
} 

Ora mi sono bloccato.

  • Devo implementare NSURLSessionDelegate.didReceiveChallenge?
  • Se sì, come gestisco il gestore di completamento?
  • In Apple Developer Riferimento ho trovato la seguente linea sotto didReceiveChallenge

    Se non implementare questo metodo, la sessione chiama URLSession dei suoi delegati: compito: didReceiveChallenge: completionHandler: invece il metodo.

  • Cosa significa?

risposta

7

Sì,

Se non implementare il metodo NSURLSessionDelegate.didReceiveChallenge, la sessione chiama URLSession dei suoi delegati: compito: didReceiveChallenge: completionHandler: invece il metodo.

Meglio implementare sia

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


}