gpt4 book ai didi

ios - 未调用 didReceive 质询身份验证方法

转载 作者:行者123 更新时间:2023-11-28 08:18:50 25 4
gpt4 key购买 nike

以下是我的代码。我正在尝试使用 didReceive 质询方法进行身份验证。 Apple 文档说,如果 session 任务需要身份验证,并且没有可用的有效凭据,则会调用“didReceive challenge”方法。但在这种情况下,它没有被调用。任何建议将被认真考虑。谢谢:)

func getServerResponse(){
var request=URLRequest(url: URL(string: "http://dev.example.com/Api/Account")!)
let configuration=URLSessionConfiguration.default
request.httpMethod="GET"
let task=URLSession.init(configuration: configuration).dataTask(with: request, completionHandler: {(data,response,error) -> Void in

do {
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
print("Result-->\(jsonResult)")
print((response as! HTTPURLResponse).statusCode)
}
} catch let error as NSError {
print(error.localizedDescription)
}
})

task.resume()

}

func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let crdential = URLCredential.init(user:"userName", password: "password", persistence: URLCredential.Persistence.none)
completionHandler(URLSession.AuthChallengeDisposition.useCredential, crdential)

}

最佳答案

委托(delegate)没有被调用,因为你没有首先设置委托(delegate)......

使用这个方法:

let task = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)

此外,设置 URLSessionDataDelegate 并符合协议(protocol):

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Swift.Void)

而不是将 dataTask 与完成处理程序一起使用,因为当您使用该方法时,它不会调用任何委托(delegate)方法。

关于ios - 未调用 didReceive 质询身份验证方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41827815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com