我知道这个问题经常被问到,但是我已经实施了答案中找到的解决方案,但是我没有任何运气。我不是Swift开发人员,所以我想我缺少了一些东西。
这是我的NSURLConnectionDelegate方法:
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool {
return protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
}
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
print("Second attempt resulted in authentication challenge")
challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge)
challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge)
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
print("We got a response.....")
}
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
print("============ second attempt failed ==============")
print("\(error)")
}
连接通过不允许DNS的VPN进行。我们必须使用IP,这会导致无效的证书错误。该证书是正确的,只是不使用IP。我试图忽略证书错误,但仍收到“发生SSL错误,无法建立与服务器的安全连接”
我的理解是
challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge)
challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge)
应该解决此问题,但似乎无法正常工作。打印行确实在运行,因此正在调用委托方法。
谢谢!
正如Breek所建议的那样,info.plist中的NSAppTransportSecurity为iOS 9带来了成功。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句