This question already has answers here:
Transport security has blocked a cleartext HTTP

(27个答案)


4年前关闭。




因此,昨晚发布的iOS新Beta Beta SDK具有“应用程序传输安全性”,可鼓励开发人员使用https而非http。原则上,这是个好主意,我已经在舞台/生产环境中使用了https。但是,当iOS应用连接到Web服务时,我没有在本地开发环境中设置https,而是在笔记本电脑上运行。

从今天早上开始的一些 Activity 来看,URL加载系统似乎将决定使用https来代替HTTP URL,即使您将其递为http URL。有谁知道如何禁用此行为-即使仅针对特定的URL?

最佳答案

有关详细信息,请参见Apple的Info.plist reference(感谢@ gnasher729)。

您可以在Info.plist中为特定域添加例外:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>testdomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>

每个例外域的所有键都是可选的。发言人没有详细说明任何按键,但是我认为它们都很明显。

(来源:WWDC 2015 session 703, “Privacy and Your App”,30:18)

如果您的应用程序有充分的理由这样做,那么您也可以使用一个键来忽略所有应用程序传输安全性限制:
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

如果您的应用程序没有充分的理由,则可能会遭到拒绝:

将NSAllowsArbitraryLoads设置为true可以使其正常工作,但是Apple非常清楚地表示,他们打算拒绝没有特定原因使用此标志的应用程序。我想到的使用NSAllowsArbitraryLoads的主要原因是用户创建的内容(链接共享,自定义Web浏览器等)。并且在这种情况下,Apple仍然希望您包括对您控制的URL强制实施ATS的例外。

如果确实需要访问未通过TLS 1.2提供的特定URL,则需要为这些域编写特定的异常,而不要使用设置为yes的NSAllowsArbitraryLoads。您可以在NSURLSesssion WWDC session 中找到更多信息。

在共享NSAllowsArbitraryLoads解决方案时请小心。这不是Apple推荐的修复程序。

kcharwood(感谢@ marco-tolman)

08-06 04:07