2017-06-02 66 views
0

下面是提出请求的代码片段。Swift 3中的URLSession - URLAuthenticationChallenge

 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) 

回答

0

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

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

你有一些指导如何正确使用该功能? – nyxee

相关问题