【问题标题】:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.?无法加载资源,因为应用程序传输安全策略需要使用安全连接。?
【发布时间】:2024-01-02 03:32:01
【问题描述】:

我什至添加了 info.plist 文件。但似乎没有任何效果。因此,我在使用 Alamofire 连接 API 时遇到问题。

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <false/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>"https://test.idelivr.com"</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
    </dict>

【问题讨论】:

标签: swift swift2 ios9


【解决方案1】:

实际上我试图在错误的 info.plist 文件中进行更新。我的错:(这是我添加的代码,现在对我来说效果很好

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

【讨论】:

    【解决方案2】:

    尝试将其添加到 info.plist 的根目录中

    <?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>
        <true/>
    </dict>
    

    将任意负载设置为 true。

    【讨论】:

    • 使用NSAllowsArbitraryLoads=true 是一种解决方法应用传输安全,它不是通用答案。在我们的例子中,OP 似乎想在他们的域中使用 https。
    最近更新 更多