【发布时间】:2019-10-20 03:23:43
【问题描述】:
我有一个应用程序,它使用自签名证书与本地托管服务器上的 API 进行交互。我在 info.plist 中包含了允许任意加载的代码,还包含了当我们收到挑战时的 URLSessionDelegate。
我在 urlsession 函数中有一个打印行,并且该行永远不会被打印,所以我觉得该代码永远不会被调用。我在我的班级中继承了 URLSessionDelegate。这一切都适用于 Swift 4。
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
//accept all certs when testing, perform default handling otherwise
print("Accepting cert as always")
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
这是收到的错误:
2019-06-04 09:59:55.604023-0500 The MUT[8866:1617557] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
2019-06-04 09:59:55.604056-0500 The MUT[8866:1617557] Task <F8D75C99-49ED-45BE-B764-942B748942DB>.<1> HTTP load failed (error code: -1202 [3:-9843])
2019-06-04 09:59:55.604150-0500 The MUT[8866:1617558] Task <F8D75C99-49ED-45BE-B764-942B748942DB>.<1> finished with error - code: -1202```
【问题讨论】:
标签: swift nsurlsession app-transport-security swift5