iOS9收到错误"发生了SSL错误,无法与服务器建立安全连接"

Nan*_*nda 102 ssl-certificate ios app-transport-security

自从我使用iOS 9升级现有项目以来,我一直收到错误:

发生SSL错误,无法与服务器建立安全连接.

Ton*_*RAN 109

对于iOS9,Apple作为App Transport Security(ATS)的一部分,在iOS 9中做出了一个激进的决定,禁用了来自iOS应用程序的所有不安全的HTTP流量.

要简单地禁用ATS,您可以通过打开Info.plist来执行此步骤,并添加以下行:

<key>NSAppTransportSecurity</key>
  <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
  </dict>
Run Code Online (Sandbox Code Playgroud)

  • 我在plist中添加了上面的行,但仍然出现以下错误:发生了SSL错误,无法建立与服务器的安全连接.NSLocalizedRecoverySuggestion =你想连接到服务器吗?,_kCFNetworkCFStreamSSLErrorOriginalValue = -9819我正在调用HTTPS请求.HTTPS还有其他选择吗? (36认同)
  • 这似乎是一个常见的误解,NSAllowsArbitraryLoads是一个启用或禁用ATS的开关.一旦执行https://请求,您必须确保满足ATS要求:服务器上安装的有效证书(没有通配符,与服务器的域名完全匹配),服务器支持TLS 1.2 with forward保密. (4认同)
  • AiOsN = 同样的问题,你找到解决方案了吗? (2认同)

Sté*_*ert 59

即使允许任意load(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>
Run Code Online (Sandbox Code Playgroud)

  • 我试图访问的服务器是使用TLSv1.0.我不得不为tls1.0添加NSExceptionMinimumTLSVersion以绕过SLL错误 (3认同)
  • 比忽略所有安全问题好多了.亚马逊的S3服务使用弱加密,直接加载到他们的服务器导致了问题,但这使我们只能打开s3.amazonaws.com网址,保持其余内联.真棒! (2认同)

Tec*_*eko 15

iOS 9强制使用HTTPS的连接为TLS 1.2以避免最近的漏洞.在iOS 8中,甚至支持未加密的HTTP连接,因此旧版本的TLS也没有出现任何问题.作为解决方法,您可以将此代码段添加到Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)

*参考App Transport Security(ATS)

在此输入图像描述


Nat*_*ble 14

如果您只是针对特定域,可以尝试将其添加到应用程序的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>
Run Code Online (Sandbox Code Playgroud)


ste*_*eve 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)
Run Code Online (Sandbox Code Playgroud)

现在尝试google(因为他们当然有SHA-256证书):

location.href = "https://google.com"
// no problemo
Run Code Online (Sandbox Code Playgroud)

添加运输安全性的例外(如上面的@stéphane-bruckert的回答所述)可以解决这个问题.我还假设完全禁用NSAppTransportSecurity也会起作用,虽然我已经读过完全禁用它可能会危及您的应用审核.

[编辑]我发现简单地枚举我在NSExceptionDomainsdict中连接的域修复了这个问题,即使将NSExceptionAllowsInsecureHTTPLoadsset设置为true也是如此.:\