根据以下错误消息,我需要在info.plist中设置什么来启用HTTP模式?
传输安全性已阻止明文HTTP(HTTP://)资源因为它是不安全的。可以通过以下方式配置临时异常应用程序的Info.plist文件。
假设我的域名是example.com。
根据以下错误消息,我需要在info.plist中设置什么来启用HTTP模式?
传输安全性已阻止明文HTTP(HTTP://)资源因为它是不安全的。可以通过以下方式配置临时异常应用程序的Info.plist文件。
假设我的域名是example.com。
当前回答
我不喜欢直接编辑plist。您可以使用GUI轻松地将其添加到plist:
单击左侧导航器中的Info.plist。现在更改主区域中的数据:在最后一行添加+输入组的名称:应用程序传输安全设置右键单击组并选择“添加行”输入允许任意加载将右侧的值设置为YES
其他回答
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>
这可能有点不安全,但我找不到其他解决方案。
转到您的Info.plist
右键单击空白区域,然后单击“添加行”将密钥名称写为NSAppTransportSecurity,在其下选择例外域,将新项目添加到此写下需要访问的域名将域类型从字符串更改为字典,添加新项NSTemporaryExceptionAllowsInsecureHTTPLoads,这将是一个具有真值的布尔值。
请参阅论坛帖子应用程序传输安全?。
例如,您可以添加特定域,如:
<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文件,因此您可以将这些代码或多或少地放在文件中的任何位置。
使用NSAppTransportSecurity:
您必须在info.plist文件中的NSAppsTransportSecurity字典下将NSAllowsArbitraryLoads键设置为YES。
⛔️ 不要使用不良做法!
许多答案(包括公认的答案)都告诉你要让你的应用程序的网络通信完全不安全!通过将“允许任意加载”设置为“是”(或true)。这是网络请求最危险的设置!它仅用于测试和临时用途。
你可以看到这位苹果工程师在WWDC18中清楚地说了这句话,即使是针对Web内容,你也在努力让他们都这样做!
✅ 将“允许任意加载”设置为NO!!!
您必须始终使用HTTPS进行网络连接。但如果真的不能,只需在info.plist中添加一个例外
例如,如果您正在使用http://google.com如果出现错误,您必须将其更改为https://google.com(带s),因为它支持得很好。
但是,如果你不知怎么做不到,(而且你无法说服后端开发人员支持SSL),就把这个不安全的域添加到info.plist中(而不是让它适用于所有不安全的网络!)