Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

swift3

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

  • App Transport Security issue in iOS 9 and iOS 10
  • http://iosdevtips.co/post/121756573323/ios-9-xcode-7-http-connect-server-error
  • http://www.neglectedpotential.com/2015/06/working-with-apples-application-transport-security/

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

like image 531
a.ajwani Avatar asked Mar 02 '17 10:03

a.ajwani


1 Answers

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.

like image 149
a.ajwani Avatar answered Nov 20 '22 01:11

a.ajwani