我们如何在一个应用程序中同时使用 HTTP 和 HTTPS ATS(应用程序传输安全)?

How can we use HTTP and HTTPS both ATS (App Transport Security) in One Application?

Apple announced “App Transport Security” for iOS 9 and OSX 10.11 El Capitan. The “What’s New in iOS” guide for iOS 9 explains:

App Transport Security (ATS) lets an app add a declaration to its Info.plist file that specifies the domains with which it needs secure communication. 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.

如果我们想要删除或禁用 ATS 意味着我们只想使用 HTTP,那么我们将在 .plist 文件中进行输入,如下所示:

<key>NSAppTransportSecurity</key>
<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>

如果我们的域是 HTTPS,我们将在 .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 HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

问题是:

如果我的应用程序仅在 HTTP 等 Web 服务上运行。我想使用 HTTPS 域,例如 google 地图或 Facebook 登录等。或者任何类似一个 Web 服务的东西都适用于 HTTPS 域。

那么我们如何在.plist文件中合并这两个东西?

如果您的应用程序 (例如第三方 Web 浏览器) 需要加载任意内容,Apple 提供了一种完全禁用 ATS 的方法,但我认为这对您要谨慎使用此功能:

完全禁用 ATS。只需在您的 Info.plist 文件中包含以下内容,然后您就可以在一个应用程序

中使用 HTTPHTTPS
<key>NSAppTransportSecurity</key>
<dict>
      <!--Include to allow all connections (DANGER)-->
      <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>

希望对您有所帮助!