首页 文章

如何在iOS 9中启用App Transport Security加载HTTP URL? [重复]

提问于
浏览
392

这个问题在这里已有答案:

因此,昨晚发布的iOS新测试版SDK具有“App Transport Security”,鼓励开发人员使用https而不是http . 原则上,这是一个好主意,我已经在我们的临时/ 生产环境 环境中使用https . 但是,当iOS应用程序连接到我在笔记本电脑上运行的Web服务时,我在本地开发环境中没有设置https .

从今天早上的一些游戏中可以看出,URL加载系统即使您将其设为http URL,也会决定使用https . 有谁知道如何禁用此行为 - 即使只是针对特定的URL?

8 回答

  • 15

    我已解决为plist文件 .

    • 添加NSAppTransportSecurity:Dictionary .

    • 将名为" NSAllowsArbitraryLoads "的子项添加为布尔值:是

    enter image description here

  • 643

    有关详细信息,请参阅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会话中找到更多信息 . 请小心共享NSAllowsArbitraryLoads解决方案 . 这不是Apple的推荐修复 .

  • 50

    已接受的答案提供了所需信息,以及有关使用和disabling App Transport Security one can find more on this的更多信息 .

    对于Per-Domain Exceptions,将这些添加到 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 HTTP requests-->
          <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
          <true/>
          <!--Include to specify minimum TLS version-->
          <key>NSTemporaryExceptionMinimumTLSVersion</key>
          <string>TLSv1.1</string>
        </dict>
      </dict>
    </dict>
    

    But What If I Don’t Know All the Insecure Domains I Need to Use?Info.plist 中使用以下键

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

    For more detail you can get from this link.

  • 29

    关注this .

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

    • 打开我的Projects info.plist 文件

    • 添加了一个名为 NSAppTransportSecurity 的密钥作为 Dictionary .

    • 添加了一个名为 NSAllowsArbitraryLoads 的子键为 Boolean ,并将其值设置为 YES ,如下图所示 .
      enter image description here

    清理项目,现在一切正常,就像以前一样 .

    参考Link .

  • 19

    如果您只想为本地开发服务器禁用应用传输策略,则以下解决方案可以正常运行 . 当您无法或无法设置HTTPS时(例如,在使用Google App Engine开发服务器时),此功能非常有用 .

    正如其他人所说的,对于 生产环境 应用程序,绝对不能关闭ATP .

    1)使用不同的plist进行调试

    复制您的Plist文件和NSAllowsArbitraryLoads . 使用此Plist进行调试 .

    XCode Debug

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

    2)排除本地服务器

    或者,您可以使用单个plist文件并排除特定服务器 . 但是,it doesn't look like you can exclude IP 4 addresses因此您可能需要使用服务器名称(可在系统首选项 - >共享中找到,或在本地DNS中配置) .

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>server.local</key>
            <dict/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
    
  • 26

    上面的配置对我不起作用 . 我尝试了很多键组合,这个工作正常:

    enter image description here

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>mydomain.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
            </dict>
        </dict>
    </dict>
    
  • 98

    编译@adurdin和@User给出的答案

    将以下内容添加到您的info.plist并使用相应的域名更改 localhost.com ,您也可以添加多个域:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>localhost.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <false/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <false/>
                <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>
    </plist>
    

    你info.plist必须如下所示:

    enter image description here

  • 12

    这对我有用:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <false/>
        <key>NSExceptionDomains</key>
        <dict>
            <key><!-- your_remote_server.com / localhost --></key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <true/>
            </dict>
        <!-- add more domain here -->
        </dict>
    </dict>
    

    我只是想添加它来帮助别人并节省一些时间:

    如果您正在使用: CFStreamCreatePairWithSocketToHost . 确保你的 host 与_689857中的内容相同,或者如果你有单独的socket域,只需将其添加到那里 .

    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)/*from .plist*/, (unsigned int)port, &readStream, &writeStream);
    

    希望这有用 . 干杯 . :)

相关问题