137

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

4

13 回答 13

240

找到解决方案:

在 iOS9 中,ATS 在网络调用期间强制执行最佳实践,包括使用 HTTPS。

来自 Apple 文档:

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

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

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

在此处输入图像描述

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

更新2:有关更多详细信息,请参阅以下链接: 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>
于 2015-06-10T05:56:34.883 回答
54

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

正如苹果所说: 在此处输入图像描述 在此处输入图像描述

在此处输入图像描述

iOS 9 和 OSX 10.11 要求您计划从中请求数据的所有主机都使用 TLSv1.2 SSL,除非您在应用程序的 Info.plist 文件中指定异常域。

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

于 2015-06-15T02:45:56.830 回答
41

Apple 关于 App Transport Security 的技术说明非常方便;它帮助我们找到了更安全的解决方案来解决我们的问题。

希望这会对其他人有所帮助。我们在连接似乎完全有效的 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>
于 2015-09-10T16:15:54.923 回答
7

如果您像我一样在使用 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

于 2015-10-15T22:52:48.847 回答
5

我从这里找到了解决方案。 它为我工作。

检查这个,它可能对你有帮助。

<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>
于 2015-06-12T10:21:20.690 回答
4

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

在此处输入图像描述

语法如下所示:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
于 2015-12-10T10:46:49.473 回答
2

更新:

从 Xcode 7.1 开始,您无需NSAppTransportSecurityinfo.plist.

它现在将为您自动完成,意识到它是一本字典,然后Allows Arbitrary也自动完成加载。 info.plist 截图

于 2015-10-22T16:01:04.567 回答
2

解决 NSURLConnection Http load failed bug 只需在 info.plist 中添加如下 Dict:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSAllowsArbitraryLoadsInWebContent</key>
        <true/>
    </dict>
于 2017-05-04T15:15:31.157 回答
1

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

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

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

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

清理项目,现在一切都像以前一样运行良好。

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

于 2015-09-18T12:51:47.340 回答
1

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

<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>
于 2016-03-11T20:30:38.587 回答
1

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

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

于 2017-04-25T15:02:28.293 回答
1

除了上面提到的答案,重新检查你的网址

于 2018-01-04T07:47:57.663 回答
0

您应该添加App Transport Security Settingsinfo.plist并添加Allow Arbitrary LoadsApp Transport Security Settings

在此处输入图像描述

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
于 2019-09-20T12:00:36.700 回答