2017-10-13 37 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) 
     } 
    }