如何将 NSAppTransportSecurity 添加到我的 info.plist 文件中?

How can I add NSAppTransportSecurity to my info.plist file?

https://developer.apple.com/videos/wwdc/2015/?id=711 @5:55

我似乎无法将此添加到我的 info.plist。没有它的价值。我是 运行 XCode 7.0 版测试版 (7A121l),正在 iOS9.

上进行测试

因为我不能像视频中看到的那样具体声明我想要的 URL,所以我不断收到 "App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file" 错误。

不过,我好像配置不了。有什么想法吗?

试试这个 --- 在 Xcode-beta 4 7.0

中为我工作
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>yourdomain.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>

还有一个选项,如果你想禁用 ATS,你可以使用这个:

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

但完全不推荐这样做。服务器应该有 SSL 证书,这样就不会泄露隐私。

这对我不起作用,但这确实有效:

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

只是为了澄清... 您应该始终使用 httpS

但是你可以绕过它添加例外:

您只需在 info.plist 文件的 NSAppTransportSecurity 字典中将 NSAllowsArbitraryLoads 键添加到 YES。

例如,

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

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>com</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
        <key>net</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
        <key>org</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

这将允许连接到 .com .net .org

更新答案(wwdc 2016 之后):

IOS apps will require secure HTTPS connections by the end of 2016

应用程序传输安全性或 ATS 是 Apple 在 iOS 9 中引入的一项功能。启用 ATS 后,它会强制应用程序通过 HTTPS 连接而不是非安全 HTTP 连接到 Web 服务。

但是,开发人员仍然可以关闭 ATS,并允许他们的应用通过 HTTP 连接发送数据,如上述答案中所述。到 2016 年底,Apple 将ATS 强制要求 所有希望将其应用程序提交到 App Store 的开发者。 link

进一步解释 ParaSara 的回答:App Transport security 成为强制性的,尝试关闭它可能会导致您的应用程序被拒绝。

作为开发人员,如果您的网络代码无法使用 App Transport 安全性,并且您希望在解决任何问题之前继续进行其他开发,则可以关闭 App Transport 安全性。比如说在一个五人团队中,四个人可以继续做其他事情,而一个人可以解决所有问题。如果您遇到网络问题并且想检查它们是否是由 App Transport 安全引起的,您也可以将 App Transport 安全作为调试工具关闭。一旦你知道,你应该立即再次打开它。

您以后必须使用的解决方案是根本不使用http,除非您使用不支持https 的第三方服务器。如果你自己的服务器不支持 https,Apple 就会有问题。即使使用第三方服务器,我也不会打赌 Apple 会接受它。

与服务器安全的各种检查相同。在某些时候,Apple 只会接受合理的例外情况。

但大多数情况下,考虑一下:您正在危及客户的隐私。在我看来,这是一个很大的禁忌。不要那样做。修复您的代码,不要请求 运行 不安全代码的许可。

对使用 NSAppTransportSecurity 的开发人员来说是个坏消息。

UPDATE:
[Apple will require HTTPS connections for iOS apps by the end of 2016]

https://techcrunch.com/2016/06/14/apple-will-require-https-connections-for-ios-apps-by-the-end-of-2016/

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>uservoice.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
            </dict>
        </dict>
    </dict>

Xcode8.2,iOS10

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

在mac shell命令行中,使用以下命令:

plutil -insert NSAppTransportSecurity -xml "<array><string> hidden </string></array>" [location of your xcode project]/Info.plist 

该命令会将所有必要的值添加到您的 plist 文件中。

XCODE 8 个,Swift 3 个: 您需要添加一行:**

"App transport Security Setting"

** 在 info.plist 内部信息 属性 列表中。

使用您喜欢的任何编辑器打开项目的 info.plist 文件,然后在文件末尾最后一个

之前添加此代码
<key>NSAppTransportSecurity</key>
<dict>
   <key>NSAllowsArbitraryLoads</key>
<true/>
</dict>