简体   繁体   中英

FAILURE: Error Domain=NSURLErrorDomain Code=-1004 “Could not connect to the server.”

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

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.

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>

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. :)

Thanks

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

Resolved for me

I have also faced this same issue and now fixed. When I stopped and started my aws instance the connection string have changed. You can find this changed connection string on your aws after clicking on connect button. Then replace old url by this new one to your AppDelegateo class (already have one)

Hope got it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM