Swift 4 TIC SSL信任错误

时间:2017-10-13 10:45:33

标签: swift ssl ios11

我编写了这个例子,但使用了另一台服务器。

http://andrewmarinov.com/parsing-json-swift-4/

我将收到一个Json文件,但是我收到错误: TIC SSL信任错误 SURLSession / NSURLConnection HTTP加载失败

我无法更改服务器上的任何内容!

我可以在swift 4 / IOS11中使用一些代码来修复它吗? 这是我改变的Plist:

<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

谢谢!

1 个答案:

答案 0 :(得分:1)

我写了这段代码,他们为我工作

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate) {
            completionHandler(.rejectProtectionSpace, nil)
        }
        if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
            let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
            completionHandler(.useCredential, credential)
        }
    }
相关问题