以下是提出请求的代码段。

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


发出此请求时,不会调用下面描述的身份验证质询方法。

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

最佳答案

尝试使用以下标题与问题中列出的标题略有不同。

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

关于ios - Swift 3中的URLSession-URLAuthenticationChallenge,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44322845/

10-10 02:07