我有一个应用程序,该应用程序使用自签名证书与本地托管服务器上的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```
答案 0 :(得分:0)
此问题已解决。我在会话声明中添加了委托:self,委托队列:myOpQueue。
let myOpQueue = OperationQueue()
...
let session = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: myOpQueue)
答案 1 :(得分:0)
请确保您已正确签名您的委托,或尝试在ViewController的viewDidLoad中对其进行签名。
此错误:HTTP加载失败是这样解决的:
NSAppTransportSecurity-> NSAllowsArbitraryLoads = true