允许任意负载在 Swift 5 次转换后不起作用

Allow Arbitrary Loads not working after Swift 5 conversion

我有一个应用程序可以使用自签名证书与本地托管服务器上的 API 进行交互。我在 info.plist 中包含了允许任意加载的代码,并且还包含了 URLSessionDelegate 以便我们在收到质询时使用。

我在 urlsession 函数中有一个打印行,但该行从未被打印过,所以我觉得该代码从未被调用过。我在 class 中继承了 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```

已解决。我在会话声明中添加了 delegate: self, delegateQueue: myOpQueue。

let myOpQueue = OperationQueue()
...
let session = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: myOpQueue)

确保您已正确签署您的委托或尝试在您的 ViewController 的 viewDidLoad 中签署它。

这个erro: HTTP Load Failed 是这样解决的:

NSAppTransportSecurity -> NSAllowsArbitraryLoads = true