iOS9获取错误“;发生SSL错误,无法与服务器建立安全连接”;

iOS9获取错误“;发生SSL错误,无法与服务器建立安全连接”;,ios,ssl-certificate,app-transport-security,Ios,Ssl Certificate,App Transport Security,自从我用iOS 9升级了我的现有项目后,我一直收到以下错误: 发生SSL错误,无法与服务器建立安全连接 对于iOS9,苹果在iOS 9上做出了一个激进的决定,作为其应用程序的一部分,禁用iOS应用程序中所有不安全的HTTP流量 要简单地禁用ATS,您可以按照以下步骤打开Info.plist,并添加以下行: <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryL

自从我用iOS 9升级了我的现有项目后,我一直收到以下错误:

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


对于iOS9,苹果在iOS 9上做出了一个激进的决定,作为其应用程序的一部分,禁用iOS应用程序中所有不安全的HTTP流量

要简单地禁用ATS,您可以按照以下步骤打开Info.plist,并添加以下行:

<key>NSAppTransportSecurity</key>
  <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
  </dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads

即使允许任意加载(
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>
NSAppTransportSecurity
NSExceptionDomains
yourserver.com
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
NSTemporaryExceptionMinimumTLSVersion
TLSv1.1

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

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>example.com</key>
        <dict>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
*提及


问题在于服务器端的ssl证书。可能有干扰,或者证书与服务不匹配。例如,当您使用的服务在myservice.mydomain.com上运行时,站点拥有www.mydomain.com的ssl证书。这是另一台机器。

似乎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)
现在试试谷歌(因为他们当然有SHA-256证书):

向传输安全性添加一个异常(如上面@stéphane bruckert的回答所述)可以解决这个问题。我还假设完全禁用
NSAppTransportSecurity
也会起作用,尽管我已经读到完全禁用它会危及你的应用程序审查


[编辑]我发现,在
NSExceptionDomains
dict中简单地枚举我连接的域可以解决这个问题,即使将
NSExceptionAllowsInsecureHTTPLoads
设置为true也是如此\

如果您只是针对特定的域,您可以尝试将其添加到应用程序的Info.plist中:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>example.com</key>
        <dict>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>
NSAppTransportSecurity
NSExceptionDomains
example.com
NSExceptionRequiresForwardSecretary
n包括多个域

当我将HTTPS URL指定为:。但是,当我在没有三个W的情况下指定它时,它可以正常工作

Xcode项目->转到info.plist并单击+按钮,然后添加(应用程序传输安全设置)展开,允许任意加载设置为是。谢谢

我的问题是
NSURLConnection
,它在iOS9中被弃用了,所以我将所有API都更改为
nsursession
,这就解决了我的问题


在我的例子中,我在模拟器中遇到了这个问题,因为我的计算机日期落后于当前日期。所以,当您遇到SSL错误时,也要检查此情况。

我在播放时遇到了以下错误

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>
完成时出现错误[-1200]error Domain=NSURLErrorDomain code=-1200“发生SSL错误,无法与服务器建立安全连接。”UserInfo={NSErrorFailingURLStringKey=https://remote-abcabc-svc.an.abc.com:1935/abr/_definst_/smil:v2/video/492F2F82592F59EA74ABAA6B9D6E6F42/F6B1BD452132329FBACD32730862CAE0/091EAD80FE9BEDD52A2F33840CA3CBAC.v3.eng.smil/playlist.m3u8,nsLocalizedRecoverysSuggestion=是否仍要连接到服务器?,\u kCFStreamErrorDomainKey=3,\u nsurErrorFailURLSessionAskerrokery=LocalDataTask.,\u NSURLErrorRelatedURLSessionTaskErrorKey=(
“LocalDataTask
我确保我在plist文件中的异常域中添加了条目,并且NSAllowsArbitraryLoads设置为true,但仍然看到了一个错误

然后我意识到我在用https而不是http来播放URL


我将视频url设置为http并解决了问题。

我在一些网络呼叫中收到此错误,而不是其他呼叫。我连接到公共wifi。该免费wifi似乎篡改了某些url,因此出现了错误


当我连接到LTE时,该错误消失了!

我在plist中添加了以上几行,但仍然得到以下错误:发生了SSL错误,无法与服务器建立安全连接。nsLocalizedRecoverysSuggestion=是否仍要连接到服务器?,\u KCFnetworkCFstreamsSleroriginalValue=-9819我正在调用HTTPS请求est.HTTPS还有其他选项吗?AiOsN=这里有相同的问题,您找到解决方案了吗?这似乎是一种常见的误解,即NSAllowsArbitraryLoads是启用或禁用ATS的开关。在执行HTTPS://请求时,您必须确保满足ATS要求:服务器上安装了有效的证书(不带通配符,与服务器的域名完全匹配),服务器支持TLS 1.2和前向保密。@Christian这些ATS要求的来源是什么?我找不到任何关于不支持通配符证书的内容。@Bartosz抱歉,我无法向您指出官方文档,这只是我们在更新服务器基础结构时观察到的内容。这比忽略所有安全性要好得多问题。Amazon的S3服务使用弱加密,直接加载到他们的服务器会导致问题,但这允许我们只打开S3.amazonaws.com url,保持其余的内联。太棒了!我正在尝试的服务器