根据以下错误消息,我需要在info.plist中设置什么来启用HTTP模式?
传输安全性已阻止明文HTTP(HTTP://)资源因为它是不安全的。可以通过以下方式配置临时异常应用程序的Info.plist文件。
假设我的域名是example.com。
根据以下错误消息,我需要在info.plist中设置什么来启用HTTP模式?
传输安全性已阻止明文HTTP(HTTP://)资源因为它是不安全的。可以通过以下方式配置临时异常应用程序的Info.plist文件。
假设我的域名是example.com。
当前回答
在Swift 5中,我们有两种方法来解决这个问题。我们需要在info.plist中添加NSAppTransportSecurity
我给出了info.plist源代码和图片以供参考
第一个是在info.plist中添加NSAppTransportSecurity->NSAllowsArbitraryLoads。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
</dict>
</dict>
</plist>
另一种方法是在info.plist中添加NSAppTransportSecurity->NSExceptionDomains,并添加URL的域,并启用加载子域(NSIncludesSubdomains)和允许不安全HTTP加载(NSExceptionAllowsInsecureHTTPLoads)的权限
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>www.7timer.info</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
</dict>
</plist>
其他回答
也许值得一提的是如何到达那里。。。
Info.plist是Main.storyboard或viewController.swift下面的文件之一。
当您第一次单击它时,它通常是表格格式的,因此右键单击该文件并“打开为”源代码,然后将下面的代码添加到末尾,即:
<key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict>
复制粘贴代码
"</dict>
</plist>"
这是在最后。
使用NSExceptionDomains可能不会同时应用效果,因为目标站点可能会通过http从外部域加载资源(例如js文件)。它也可以通过将这些外部域添加到NSExceptionDomains来解决。
若要检查哪些资源无法加载,请尝试使用远程调试。这里有一个教程:http://geeklearning.io/apache-cordova-and-remote-debugging-on-ios/
有两种解决方案:
解决方案1:
在Info.plist文件中添加密钥为“NSAppTransportSecurity”的字典在字典中添加另一个关键字为“允许任意加载”的元素
Plist结构应如下图所示。
解决方案2:
在Info.plist文件中添加密钥为“NSAppTransportSecurity”的字典在字典中添加另一个键为“NSExceptionDomains”的元素添加具有NSDictionary类型的键“MyDomainName.com”的元素添加具有布尔类型的键“NSIncludesSubdomains”且值设置为YES的元素添加具有Boolean类型的键“NSTemporaryExceptionAllowsInsecureHTTPLoads”且值设置为YES的元素
Plist结构应如下图所示。
首选解决方案2,因为它只允许选定的域,而解决方案1允许所有不安全的HTTP连接。
在Swift 5中,我们有两种方法来解决这个问题。我们需要在info.plist中添加NSAppTransportSecurity
我给出了info.plist源代码和图片以供参考
第一个是在info.plist中添加NSAppTransportSecurity->NSAllowsArbitraryLoads。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
</dict>
</dict>
</plist>
另一种方法是在info.plist中添加NSAppTransportSecurity->NSExceptionDomains,并添加URL的域,并启用加载子域(NSIncludesSubdomains)和允许不安全HTTP加载(NSExceptionAllowsInsecureHTTPLoads)的权限
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>www.7timer.info</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
</dict>
</plist>
如本技术说明所述,确定可以自动执行哪些设置:
/usr/bin/nscurl --ats-diagnostics --verbose https://your-domain.com