iOS 9上的NSURLSession/NSURLConnection HTTP加载失败

iOS 9上的NSURLSession/NSURLConnection HTTP加载失败,ios,afnetworking,nsurlsession,ios9,tls1.2,Ios,Afnetworking,Nsurlsession,Ios9,Tls1.2,尝试在iOS9上运行我现有的应用程序,但在使用AFURLSessionManager时失败 __block NSURLSessionDataTask *task = [self.sessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) { if (error) { } el

尝试在iOS9上运行我现有的应用程序,但在使用
AFURLSessionManager
时失败

__block NSURLSessionDataTask *task = [self.sessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
    if (error) {

    } else {

    }
}];

[task resume];
我得到以下错误:

Error Domain=NSURLErrorDomain Code=-999 "cancelled.
还将获得以下日志:

 NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824
 CFNetwork SSLHandshake failed (-9824)
更新: 我已将多个更新添加到我的解决方案中: 找到了解决方案:

在iOS9中,ATS在网络呼叫期间实施最佳实践,包括使用HTTPS

ATS可防止意外泄露,提供安全的默认行为,且易于采用。无论您是在创建新应用程序还是在更新现有应用程序,都应尽快采用ATS。如果你正在开发一个新的应用程序,你应该只使用HTTPS。如果你有一个现有的应用程序,你现在就应该尽可能多地使用HTTPS,并尽快创建一个迁移其余应用程序的计划

在beta 1中,目前无法在info.plist中定义这一点。解决方案是手动添加:

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

更新1:在您准备采用iOS9 ATS支持之前,这是一个临时解决方案。

更新2:有关更多详细信息,请参阅以下链接:

更新3:如果您试图连接到只有TLS 1.0的主机(YOURHOST.COM)

将这些添加到应用程序的Info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>YOURHOST.COM</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>1.0</string>
            <key>NSTemporaryExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>
NSAppTransportSecurity
NSExceptionDomains
YOURHOST.COM
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
NSTemporaryExceptionMinimumTLSVersion
1
N临时例外要求转发保密
我从中找到了解决方案,并且它对我有效

检查这个,它可能会帮助你

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
         <dict>
             <key>myDomain.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
myDomain.com
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
NSTemporaryExceptionMinimumTLSVersion
TLSv1.1

如何在iOS9中处理SSL,一种解决方案是:

俗话说:

除非在应用程序的Info.plist文件中指定了例外域,否则iOS 9和OSX 10.11要求所有计划从中请求数据的主机使用TLSv1.2 SSL

Info.plist配置的语法如下所示:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow insecure HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
    <!--Connect to anything (this is probably BAD)-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
NSAppTransportSecurity
NSExceptionDomains
yourserver.com
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
NSTemporaryExceptionMinimumTLSVersion
TLSv1.1
如果您的应用程序(例如,第三方web浏览器)需要连接到任意主机,则可以如下配置:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow insecure HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
    <!--Connect to anything (this is probably BAD)-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
如果您必须这样做,那么最好更新您的服务器以使用TLSv1.2和SSL,如果它们还没有这样做的话。这应被视为临时解决办法

到目前为止,预发布文档没有以任何特定的方式提及这些配置选项。一旦完成,我将更新答案以链接到相关文档

有关更多信息,请转到

;它帮助我们找到了一个更安全的解决方案

希望这能帮助其他人。我们在连接到AmazonS3URL时遇到了问题,这些URL看起来非常有效,即TLSv12 HTTPS URL。事实证明,我们必须禁用
nsExceptionRequiresForwardSecretary
,以启用S3使用的其他少量密码

在我们的
Info.plist
中:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>amazonaws.com</key>
    <dict>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSExceptionRequiresForwardSecrecy</key>
      <false/>
    </dict>
  </dict>
</dict>
NSAppTransportSecurity
NSExceptionDomains
亚马逊网站
n包括多个域
NSExceptionRequiresForwardSecretary

我通过在info.plist中添加一些键解决了这个问题。我遵循的步骤是:

我打开了项目的info.plist文件

添加了一个名为NSAppTransportSecurity的密钥作为字典

添加了一个名为NSAllowsArbitraryLoads的子键作为布尔值,并将其值设置为YES,如下图所示。 在此处输入图像描述

清理项目,现在一切都像以前一样正常运行


Ref Link:

如果您作为我使用Amazon S3时遇到此问题,请尝试将其作为顶级标记的直接子项粘贴到info.plist上

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>amazonaws.com</key>
        <dict>
              <key>NSThirdPartyExceptionMinimumTLSVersion</key>
              <string>TLSv1.0</string>
              <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
              <false/>
              <key>NSIncludesSubdomains</key>
              <true/>
        </dict>
        <key>amazonaws.com.cn</key>
        <dict>
              <key>NSThirdPartyExceptionMinimumTLSVersion</key>
              <string>TLSv1.0</string>
              <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
              <false/>
              <key>NSIncludesSubdomains</key>
              <true/>
        </dict>
    </dict>
</dict>
NSAppTransportSecurity
NSExceptionDomains
亚马逊网站
第三方例外最小版本
TLSv1.0
N第三方例外要求转发保密
n包括多个域
亚马逊网站
第三方例外最小版本
TLSv1.0
N第三方例外要求转发保密
n包括多个域
有关更多信息,请访问:

更新:

从Xcode 7.1开始,您不需要在
info.plist
中手动输入
NSAppTransportSecurity
字典

现在,它将为您自动完成,意识到它是一本字典,然后自动完成
也允许任意加载。

只需在.plist文件中添加以下字段

语法如下所示:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow insecure HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
    <!--Connect to anything (this is probably BAD)-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads

当我遇到这个错误时,这就是我的工作原理:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>example.com</key>
        <dict>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
        </dict>
    </dict>
</dict>
NSAppTransportSecurity
NSExceptionDomains
example.com
NSExceptionRequiresForwardSecretary
NSTemporary ExceptionalLowsInSecureHttpLoads