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

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

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

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!