简体   繁体   English

Swift 3中的URLSession-URLAuthenticationChallenge

[英]URLSession in Swift 3 - URLAuthenticationChallenge

Below is the snippet for making request. 以下是提出请求的代码段。

        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json", forHTTPHeaderField: "Accept")
        let configurationId = String(format: "NetworkManager%d", UInt16(arc4random_uniform(UInt32(UINT16_MAX))))
        let configuration = URLSessionConfiguration.background(withIdentifier: configurationId)
        let session = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.current)
        let task = session.downloadTask(with: request)
        task.resume()

While making this request, the authentication challenge methods as described below are not getting called. 在发出此请求时,不会调用以下所述的身份验证质询方法。

@nonobjc func urlSession(_ session: Foundation.URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: (Foundation.URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void)

@nonobjc func urlSession(_ session: Foundation.URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler:  (Foundation.URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void)

Try using the following header slightly different from the ones you listed in your question. 尝试使用与您在问题中列出的标题略有不同的以下标题。

open func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    //handle challenge here.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM