ios - Swift 3 中的 URLSession - URLAuthenticationChallenge

标签 ios swift urlsession authenticationchallenge

下面是发出请求的片段。

        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/

相关文章:

ios - 通过协议(protocol)更改委托(delegate)的属性

Swift 从另一个文件调用完成处理程序失败

ios - swift - 如何从服务器获取 JSON 响应

swift - 为什么我们的 URLSession 对象是变量而不是常量

ios - 在 Xcode 中停止应用程序时自动关闭 iOS 模拟器

ios - iOS 上的 Twitter 状态/update_with_media 返回 500 错误

ios - 用于 UIWebView 的 NSURLProtocol 缓慢下载的占位符图像

ios - UITableViewCell 及其 socket 是否在 `dequeueReusableCellWithIdentifier` 之后初始化?

ios - 将字符串条目从 Swift iOS 中的 UITextField 保存到 plist

swift - 带 DispatchGroup 的并行 URLSession 请求在 1 个请求上调用完成处理程序两次