2014-07-08 16 views
7

Tôi hiện đang cố gắng thay đổi mã của mình từ việc sử dụng NSURLConnection thành NSURLSession. Một điều gây nhầm lẫn cho tôi là xác thực.Swift NSURLPhiên bản và xác thực

Dịch vụ của tôi mà tôi đang cố gắng kết nối được xác thực cơ bản.

Trong mã cũ của tôi, tôi đã có phương pháp sau đây bằng cách thực hiện giao thức 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) 
    } 
} 

Bây giờ tôi bị mắc kẹt.

  • Tôi có phải triển khai NSURLSessionDelegate.didReceiveChallenge không?
  • Nếu có, làm cách nào để xử lý với trình xử lý hoàn thành?
  • Trong Reference Developer của Apple Tôi đã tìm thấy dòng sau dưới didReceiveChallenge

    Nếu bạn không thực hiện phương pháp này, phiên gọi URLSession đại biểu của nó: nhiệm vụ: didReceiveChallenge: completionHandler: Phương pháp thay thế.

  • Điều này có nghĩa là gì?

Trả lời

7

Vâng,

Nếu bạn không thực hiện phương pháp NSURLSessionDelegate.didReceiveChallenge, phiên gọi URLSession đại biểu của nó: nhiệm vụ: didReceiveChallenge: completionHandler: Phương pháp thay thế.

Tốt hơn để triển khai cả hai

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


} 
Các vấn đề liên quan