失败:错误域=NSURLErrorDomain 代码=-1004“无法连接到服务器".

失败:错误域=NSURLErrorDomain 代码=-1004“无法连接到服务器

问题描述:

在此之前,我想列出我已阅读并尝试实现答案的帖子(避免重复):

Before anything, I want to list out the posts that I have read and tried implementing the answers from (avoiding duplicates):

我正在开发一个 SDK,我已经创建了一些 UI 测试.我试图不连接到后端系统并通过在测试时在本地机器上运行的存根服务器模拟它们.所有请求都必须发送到此服务器.

I am working on an SDK and I have created some UI tests. I am trying not to connect to the backend systems and mocking them via a stubserver that runs in the local machine whilst testing. All requests must go to this server.

我创建了一个永远不会提交到 App Store 的虚拟应用程序.通过这个应用程序,我计划测试 SDK 的屏幕.我遵循了上面的帖子和文章,并在虚拟应用程序的 Info.plist 文件中包含了一个异常,即与我的本地主机服务器的不安全连接.如下所示:

I have created an dummy app that will never be submitted to the App Store. Through this app I plan to test the SDK's screens. I followed the posts and articles above and included an exception to have insecure connections to my localhost server into the dummy app's Info.plist file. It looks like the following:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>localhost</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
        <key>NSAllowsArbitraryLoadsInWebContent</key>
        <true/>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

是的,我允许各种例外.网络响应不会抱怨应用程序没有与 localhost 的不安全连接的异常,而是简单地返回以下内容:

Yes I have allowed all kinds of exceptions. The network response does not complain about the app not having the exception for insecure connections to localhost but simply returns the following:

[Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x170248940 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://localhost:7878/endpoint, NSErrorFailingURLKey=http://localhost:7878/endpoint, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Could not connect to the server.}
[Timeline]: Timeline: { "Request Start Time": 510144201.835, "Initial Response Time": 510144201.860, "Request Completed Time": 510144201.860, "Serialization Completed Time": 510144201.860, "Latency": 0.025 secs, "Request Duration": 0.025 secs, "Serialization Duration": 0.000 secs, "Total Duration": 0.025 secs }

我查看了服务器日志,没有任何报告.因此,我假设它不会完全命中服务器.

I look at the server logs and nothing is reported. I therefore assume that it does not hit the server entirely.

任何帮助将不胜感激.:)

Any help would be appreciated. :)

谢谢

正如@Teffi 在评论中指出的那样,我在无法访问本地计算机上运行的 localhost 服务器的设备上运行测试.

As @Teffi pointed out in the comments I was running the test on a device that did not have access to my localhost server running on my local machine.