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

问题描述 投票:135回答:12

试图在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)

更新:我已经为我的解决方案添加了多个更新:NSURLSession/NSURLConnection HTTP load failed on iOS 9

ios afnetworking nsurlsession ios9 tls1.2
12个回答
239
投票

找到解决方案

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

From Apple documentation:

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

在beta 1中,目前无法在info.plist中定义此内容。解决方案是手动添加它:

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

Update1:​​这是一个临时解决方法,直到您准备好采用iOS9 ATS支持。

Update2:有关详细信息,请参阅以下链接:http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

Update3:如果您尝试连接到只有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>

0
投票

当我遇到这个错误时,这对我有用:

<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>

0
投票

您可以尝试在文件RCTHTTPRequestHandler.m中添加此功能

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]); }


0
投票

除了上述答案,请重新检查您的网址


53
投票

如何在iOS9中处理SSL,一个解决方案是这样做:

正如Apple所说:

除非您在应用的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>

如果您的应用程序(例如第三方Web浏览器)需要连接到任意主机,您可以像这样配置它:

<key>NSAppTransportSecurity</key>
<dict>
    <!--Connect to anything (this is probably BAD)-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

如果您不得不这样做,最好更新您的服务器以使用TLSv1.2和SSL,如果他们还没有这样做的话。这应该被视为临时解决方法。

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

有关更多信息,请访问iOS9AdaptationTips


39
投票

Apple's Technote on App Transport Security is very handy;它帮助我们找到了更安全的解决方案。

希望这会帮助别人。我们在连接到看似完全有效的Amazon S3 URL时遇到问题,TLSv12 HTTPS URL。事实证明,我们必须禁用NSExceptionRequiresForwardSecrecy以启用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>

6
投票

如果您在使用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>

您可以在以下位置找到更多信息

http://docs.aws.amazon.com/mobile/sdkforios/developerguide/ats.html#resolving-the-issue


4
投票

我从here.找到了解决方案并且它为我工作。

检查一下,它可能对你有帮助。

<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>

3
投票

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

enter image description here

语法如下所示:

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

1
投票

更新:

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

它现在将自动完成,意识到它是一个字典,然后自动完成Allows Arbitrary负载。 info.plist screenshot


1
投票

解决NSURLConnection Http加载失败的错误只需在info.plist中添加以下Dict:

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

0
投票

我已经通过在info.plist中添加一些键来解决它。我遵循的步骤是:

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

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

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

清洁项目,现在一切正常,就像以前一样。

参考链接:https://stackoverflow.com/a/32609970

© www.soinside.com 2019 - 2024. All rights reserved.