【问题标题】:Not allowed to make request to local host - swift不允许向本地主机发出请求 - swift
【发布时间】:2020-08-26 12:31:52
【问题描述】:

我无法在 swift 5、XCode 版本 11.6 (11E708) 和 iOS 13.6 中发布到服务器。我收到此错误:

2020-08-26 08:20:55.252545-0400 iris[1642:721953] Task <BBB3809B-7BEF-4F60-9685-774027ADA7E6>.<1> finished with error [-1022] Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x2810ad710 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://localhost:8000/searchImage/, NSErrorFailingURLKey=http://localhost:8000/searchImage/, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}
error=Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x2810ad710 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://localhost:8000/searchImage/, NSErrorFailingURLKey=http://localhost:8000/searchImage/, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}

我已经关注了这个问题How can I delete derived data in Xcode 8? 并更新了我的 info.plist

<key> App Transport Security Settings </key>
<dict>
    <key> Allow Arbitrary Loads </key>
    <true />

请注意,此时我并不关心安全性。此外,我尝试了一个干净的构建,然后删除了我的派生数据文件夹,但我仍然无法发布到服务器。在另一个项目中,我在 Info.plist 中设置了这个键,一切都很好。有什么建议吗?

【问题讨论】:

    标签: ios xcode security swift5


    【解决方案1】:

    如果您从源代码中编辑info.plist,以允许所有域,请更改以下值:

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

    那么它应该看起来像下面的属性列表:

    为了更好的方法,您可以默认禁用它并为特定域添加例外,如下所示。

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <false/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>yourdomain.com</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>
    

    【讨论】:

    • 感谢您的回复。除了将您的建议添加到 Info.plist 之外,我还必须将以下内容添加到项目权利 com.apple.security.network.server Boolean Truecom.apple.security.network.client Boolean True
    【解决方案2】:

    打开项目目标的 info.plist 文件并添加一个名为 NSAppTransportSecurity 的密钥作为字典。

    将 NSAllowsArbitraryLoads 添加为布尔值,并在 NSAppTransportSecurity 下将其值设置为 YES,如下图 -

    【讨论】:

    • 嘿阿卜杜勒 - 这看起来像是已接受答案的副本。
    猜你喜欢
    • 2016-07-16
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2021-07-01
    • 2012-03-22
    • 2018-01-28
    • 2023-02-15
    • 2018-08-28
    相关资源
    最近更新 更多