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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30739473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:18:22  来源:igfitidea点击:

NSURLSession/NSURLConnection HTTP load failed on iOS 9

iosafnetworkingnsurlsessionios9tls1.2

提问by Tariq

Tried to run my existing app on iOS9 but getting failure while using AFURLSessionManager.

试图在 iOS9 上运行我现有的应用程序,但在使用AFURLSessionManager.

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

    } else {

    }
}];

[task resume];

I get the following error:

我收到以下错误:

Error Domain=NSURLErrorDomain Code=-999 "cancelled.

Also getting following logs:

还得到以下日志:

 NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824
 CFNetwork SSLHandshake failed (-9824)

Update:I have added multiple updates to my solution: NSURLSession/NSURLConnection HTTP load failed on iOS 9

更新:我为我的解决方案添加了多个更新: iOS 9 上的 NSURLSession/NSURLConnection HTTP 加载失败

回答by Tariq

Found solution:

找到解决方案:

In iOS9, ATS enforces best practices during network calls, including the use of HTTPS.

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

From Apple documentation:

来自苹果文档:

ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt. You should adopt ATS as soon as possible, regardless of whether you're creating a new app or updating an existing one. If you're developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible.

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

In beta 1, currently there is no way to define this in info.plist. Solution is to add it manually:

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

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

enter image description here

在此处输入图片说明

Update1:This is a temporary workaround until you're ready to adopt iOS9 ATS support.

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

Update2:For more details please refer following link: http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

Update2:更多详情请参考以下链接:http: //ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

Update3:If you are trying to connect to a host (YOURHOST.COM) that only has TLS 1.0

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

Add these to your app's Info.plist

将这些添加到您的应用程序的 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>

回答by ElonChan

How to deal with the SSL in iOS9,One solution is to do like:

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

As the Applesay : enter image description hereenter image description here

正如苹果所说: 在此处输入图片说明在此处输入图片说明

enter image description here

在此处输入图片说明

iOS 9 and OSX 10.11 require TLSv1.2 SSL for all hosts you plan to request data from unless you specify exception domains in your app's Info.plist file.

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

The syntax for the Info.plist configuration looks like this:

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>

If your application (a third-party web browser, for instance) needs to connect to arbitrary hosts, you can configure it like this:

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

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

If you're having to do this, it's probably best to update your servers to use TLSv1.2 and SSL, if they're not already doing so. This should be considered a temporary workaround.

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

As of today, the prerelease documentation makes no mention of any of these configuration options in any specific way. Once it does, I'll update the answer to link to the relevant documentation.

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

For more info ,go to iOS9AdaptationTips

有关更多信息,请访问iOS9AdaptationTips

回答by Ben Kreeger

Apple's Technote on App Transport Security is very handy; it helped us find a more secure solution to our issue.

Apple 关于 App Transport Security 的 Technote 非常方便;它帮助我们找到了更安全的解决方案。

Hopefully this will help someone else. We were having issues connecting to Amazon S3 URLs that appeared to be perfectly valid, TLSv12 HTTPS URLs. Turns out we had to disable NSExceptionRequiresForwardSecrecyto enable another handful of ciphers that S3 uses.

希望这会帮助别人。我们在连接到似乎完全有效的 Amazon S3 URL TLSv12 HTTPS URL 时遇到问题。事实证明,我们必须禁用NSExceptionRequiresForwardSecrecy才能启用 S3 使用的另一些密码。

In our Info.plist:

在我们的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>

回答by David Cespedes

If you're having this problem with Amazon S3 as me, try to paste this on your info.plist as a direct child of your top level tag

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

You can find more info at:

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

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

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

回答by Ashvin Ajadiya

I found solution from here.And its working for me.

我从这里找到了解决方案它对我有用。

Check this, it may help you.

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

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

回答by geet Sebastian

Simply add the following fields in your .plist file

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

enter image description here

在此处输入图片说明

Syntax looks like this:

语法如下所示:

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

回答by Ben

Update:

更新:

As of Xcode 7.1, you don't need to manually enter the NSAppTransportSecurityDictionary in the info.plist.

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

It will now autocomplete for you, realize it's a dictionary, and then autocomplete the Allows ArbitraryLoads as well. info.plist screenshot

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

回答by Sakir Sherasiya

Solve NSURLConnection Http load failed bug Just Add following Dict in info.plist:

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

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

回答by Vaishnavi

In addition to the above mentioned answers ,recheck your url

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

回答by KVISH

This is what worked for me when I had this error:

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

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