自从我使用iOS 9升级现有项目以来,我一直收到错误:
发生SSL错误,无法与服务器建立安全连接。
答案 0 :(得分:105)
对于iOS9,Apple在iOS 9中做出了一个激进的决定,禁用了来自iOS应用的所有不安全的HTTP流量,作为App Transport Security (ATS)的一部分。
要简单地禁用ATS,您可以通过打开 Info.plist 来执行此步骤,并添加以下行:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
答案 1 :(得分:55)
即使允许任意加载(NSAllowsArbitraryLoads = true
)是一个很好的解决方法,您也不应该完全禁用ATS,而是启用您想要允许的HTTP连接:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourserver.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
答案 2 :(得分:14)
iOS 9强制使用HTTPS的连接为TLS 1.2以避免最近的漏洞。在iOS 8中,甚至支持未加密的HTTP连接,因此旧版本的TLS也没有出现任何问题。作为解决方法,您可以将此代码段添加到Info.plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
答案 3 :(得分:13)
如果您只是定位特定的域名,可以尝试将其添加到您的应用程序的Info.plist中:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key>
<dict>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
答案 4 :(得分:6)
似乎iOS 9.0.2中断了对有效HTTPS端点的请求。我目前的怀疑是它需要SHA-256证书,否则会因此错误而失败。
要重现,请使用safari检查UIWebView,然后尝试导航到任意HTTPS端点:
location.href = "https://d37gvrvc0wt4s1.cloudfront.net/js/v1.4/rollbar.min.js"
// [Error] Failed to load resource: An SSL error has occurred and a secure connection to the server cannot be made. (rollbar.min.js, line 0)
现在尝试google(因为他们当然有SHA-256证书):
location.href = "https://google.com"
// no problemo
为运输安全添加例外(正如@stéphane-bruckert上面的回答所述)可以解决这个问题。我还假设完全禁用NSAppTransportSecurity
也会起作用,但我已经读过,完全禁用它可能会危及您的应用审核。
[编辑]我发现,即使将NSExceptionDomains
设置为true,只需枚举我在NSExceptionAllowsInsecureHTTPLoads
dict中连接的域即可解决此问题。 :\
答案 5 :(得分:2)
问题是服务器端的ssl证书。任何事情都在干扰或证书与服务不匹配。例如,当一个站点拥有www.mydomain.com的ssl证书,而您使用的服务在myservice.mydomain.com上运行时。那是一台不同的机器。
答案 6 :(得分:2)
当我将HTTPS网址指定为:https://www.mywebsite.com时,我收到同样的错误。但是,当我在没有三个W的情况下指定它时,它可以正常工作:https://mywebsite.com。
答案 7 :(得分:2)
Xcode项目 - &gt;转到info.plist并单击+按钮,然后单击添加(应用程序传输安全设置)展开,允许任意载入设置为是。感谢
答案 8 :(得分:0)
我的问题是NSURLConnection
,并且在iOS9中已弃用,因此我将所有API更改为NSURLSession
,这解决了我的问题。
答案 9 :(得分:0)
就我而言,由于计算机的日期晚于当前日期,因此我在模拟器中遇到了此问题。因此,当您遇到SSL错误时,也请检查这种情况。
答案 10 :(得分:0)
我在播放时遇到了错误
finished with error [-1200] Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSErrorFailingURLStringKey=https://remote-abcabc-svc.an.abc.com:1935/abr/_definst_/smil:v2/video/492F2F82592F59EA74ABAA6B9D6E6F42/F6B1BD452132329FBACD32730862CAE0/091EAD80FE9BEDD52A2F33840CA3CBAC.v3.eng.smil/playlist.m3u8, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <692A1174-DA1C-4267-9560-9020A79F8458>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <692A1174-DA1C-4267-9560-9020A79F8458>
我确保在plist文件的异常域中添加了条目,并且NSAllowsArbitraryLoads设置为true,但仍然看到错误。
然后我意识到我正在使用https而不是http播放URL。
我将视频网址设置为http,并解决了问题。
答案 11 :(得分:0)
我在某些网络电话而不是其他网络电话中收到此错误。我已连接到公共wifi。这种免费的wifi似乎在篡改某些URL,因此会产生错误。
当我连接到LTE时,该错误消失了!