URLSession Error -1009 when offline with local server

Using XCode 10.1, Swift 4.2, iOS 11.0 Base SDK, with a simple project to invoke URLSession to hit an endpoint configured on a local, vanilla Apache server;




http://localhost/test.php



With the following App Transport Security correctly configured in my app's .plist;



<key>NSAppTransportSecurity</key>

<dict>

<key>NSAllowsArbitraryLoads</key>

<true/>

<key>NSAllowsLocalNetworking</key>

<true/>

</dict>



Using the following URLSession code;



let requestURL = URL(string: "http://localhost/test.php")



var getRequest = URLRequest(url: requestURL, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60)

getRequest.httpMethod = "GET"

getRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

getRequest.setValue("application/json", forHTTPHeaderField: "Accept")

URLSession.shared.dataTask(with: getRequest, completionHandler: { (data, response, error) -> Void in

if error != nil

{

Logging.error("GET Request: Communication error: \(error!)") // <- -1009 here

}

}

When 'online' on WiFi, with a working internet connection, all is well, and my app can access the endpoint and obtain my JSON data.



As soon as my WiFi loses internet connectivity (which happens a lot on my train commute), URLSession starts returning -1009 instantly (full error below), even though I can hit this endpoint through a local browser and view the JSON.



Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x7b0c0005f6d0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://localhost/test.php, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=50, NSLocalizedDescription=The Internet connection appears to be offline.



This also happens if I turn off WiFi. While this isn't a 'production' oriented issue AFAIK, it's sure painful and prevents software development on a commute!



At this time, I can find no way to 'tune' URLSession to work in this scenario, although I am really happy to told I am missing something...



I suspect URLSession is (internally) performing some Reachability check against a remote endpoint, and thus failing with the -1009 without honouring the actual request.



Has anyone encountered this issue / resolved it? If not, I'll raise a 'bug' report accordingly (though it's not really a bug in 'iOS platform terms').



Thanks in advance!

Answered by DTS Engineer in 341134022

Error -1009 is

NSURLErrorNotConnectedToInternet
, which is pretty much what it says on the ‘tin’.

If not, I'll raise a 'bug' report accordingly …

I think you should file a bug report about this. Access to

localhost
should never yield this error, so it shouldn’t matter how you’ve managed to get the system into this state, it’s still a bug IMO.

Please make sure to include a sysdiagnose log in your bug report. For information about that see our Bug Reporting > Profiles and Logs page.

I’d also appreciate you posting your bug number here, just for the record.

I doubt you’ll be able to work around this but I do have one suggestion worth trying, namely to switch from

localhost
to 127.0.0.1. Does that help?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

Error -1009 is

NSURLErrorNotConnectedToInternet
, which is pretty much what it says on the ‘tin’.

If not, I'll raise a 'bug' report accordingly …

I think you should file a bug report about this. Access to

localhost
should never yield this error, so it shouldn’t matter how you’ve managed to get the system into this state, it’s still a bug IMO.

Please make sure to include a sysdiagnose log in your bug report. For information about that see our Bug Reporting > Profiles and Logs page.

I’d also appreciate you posting your bug number here, just for the record.

I doubt you’ll be able to work around this but I do have one suggestion worth trying, namely to switch from

localhost
to 127.0.0.1. Does that help?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Oh my, that is embarrassing... I could have sworn one of my many desperate workarounds tried 127.0.0.1...


That solved the problem for me - thank you Quinn! The only oddity is that in this scenario, I get no traffic through my local Charles Proxy even though the app/simulator is able to talk to 127.0.0.1, however using 127.0.0.1 in a browser does show local traffic.


But, development is now unblocked! I can work on the train again 🙂


So it may be due to some internal inability to resolve 'localhost' from the DNS - and could be an apache config issue on my side perhaps, which would no longer pose a 'bug' with URLSession, IMHO. However, I will knock out a bare bones project to replicate this, and the above info and raise a bug with this in mind.


Thanks very much for the assist!

URLSession Error -1009 when offline with local server
 
 
Q