根据以下错误消息,我需要在info.plist中设置什么来启用HTTP模式?
传输安全性已阻止明文HTTP(HTTP://)资源因为它是不安全的。可以通过以下方式配置临时异常应用程序的Info.plist文件。
假设我的域名是example.com。
根据以下错误消息,我需要在info.plist中设置什么来启用HTTP模式?
传输安全性已阻止明文HTTP(HTTP://)资源因为它是不安全的。可以通过以下方式配置临时异常应用程序的Info.plist文件。
假设我的域名是example.com。
当前回答
⛔️ 不要使用不良做法!
许多答案(包括公认的答案)都告诉你要让你的应用程序的网络通信完全不安全!通过将“允许任意加载”设置为“是”(或true)。这是网络请求最危险的设置!它仅用于测试和临时用途。
你可以看到这位苹果工程师在WWDC18中清楚地说了这句话,即使是针对Web内容,你也在努力让他们都这样做!
✅ 将“允许任意加载”设置为NO!!!
您必须始终使用HTTPS进行网络连接。但如果真的不能,只需在info.plist中添加一个例外
例如,如果您正在使用http://google.com如果出现错误,您必须将其更改为https://google.com(带s),因为它支持得很好。
但是,如果你不知怎么做不到,(而且你无法说服后端开发人员支持SSL),就把这个不安全的域添加到info.plist中(而不是让它适用于所有不安全的网络!)
其他回答
也许值得一提的是如何到达那里。。。
Info.plist是Main.storyboard或viewController.swift下面的文件之一。
当您第一次单击它时,它通常是表格格式的,因此右键单击该文件并“打开为”源代码,然后将下面的代码添加到末尾,即:
<key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict>
复制粘贴代码
"</dict>
</plist>"
这是在最后。
以下是直观的设置:
2015-09-25(2015-09-18 Xcode更新后):
我用了一种非懒惰的方法,但没有奏效。以下是我的尝试。
第一
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>www.xxx.yyy.zzz</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
其次,
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>www.xxx.yyy.zzz</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
最后,我使用了懒惰的方法:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
这可能有点不安全,但我找不到其他解决方案。
请参阅论坛帖子应用程序传输安全?。
例如,您可以添加特定域,如:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>example.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>
懒惰选项是:
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
###注:
info.plist是一个XML文件,因此您可以将这些代码或多或少地放在文件中的任何位置。
开发示例
这里是一个plist的截图,它保持ATS的完整性(=安全),但允许通过HTTP而不是HTTPS连接到本地主机。它在Xcode 7.1.1中工作。