2017-10-13 38 views
4

我编码此示例,但使用另一台服务器。Swift 4 TIC SSL信任错误

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

我会收到一个JSON文件,但我得到的错误: TIC SSL信托错误 SURLSession/NSURLConnection的HTTP加载失败

我不能改变的事情服务器!

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

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

谢谢!

+0

是的,我的意思是“类JSON”,对不起 – silazzz

+0

我有同样的错误。 –

+0

如果您有权访问服务器,您可以生成一个自签名证书并将其添加到您的IIS。这对我有效! – silazzz

回答

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) 
     } 
    }