Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift NSURLConnection忽略SSL证书?_Swift_Ssl_Nsurlconnection - Fatal编程技术网

Swift NSURLConnection忽略SSL证书?

Swift NSURLConnection忽略SSL证书?,swift,ssl,nsurlconnection,Swift,Ssl,Nsurlconnection,我知道这个问题经常被问到,但我已经实现了答案中的解决方案,我没有任何运气。我不是一个敏捷的开发人员,所以我猜我错过了一些东西 以下是我的NSURLConnectionLegate方法: func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool { return protectionS

我知道这个问题经常被问到,但我已经实现了答案中的解决方案,我没有任何运气。我不是一个敏捷的开发人员,所以我猜我错过了一些东西

以下是我的NSURLConnectionLegate方法:

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提供了窍门。

正如Breek所建议的,info.plist中的NSAppTransportSecurity为iOS 9提供了窍门。

您是否尝试将
NSAppTransportSecurity
添加到您的info.plist中?您是否尝试将
NSAppTransportSecurity
添加到您的info.plist中?