[:en]Swift: error=Optional(Error Domain=NSURLErrorDomain Code=-1005[:]

[:en]Trouble shooting

error=Optional(Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” UserInfo={NSErrorFailingURLStringKey=http://

Solution
1. Restart simulator OR

2. Delete the app on iPhone or iPad. OR

3. As far as we can tell, when iOS 8 receive an HTTP response with a Keep-Alive header, it keeps this connection to re-use later (as it should), but it keeps it for more than the timeout parameter of the Keep-Alive header and then when a second request comes it tries to re-use a connection that has been dropped by the server.

Here are the solutions we have found so far:

Increase the timeout parameter of the server above 30 seconds. It looks like iOS is always behaving as if the server will keep the connection open for 30 seconds regardless of the value provided in the Keep-Alive header. (This can be done for Apache by setting the KeepAliveTimeout option.
You can simply disable the keep alive mechanism for iOS clients based on the User-Agent of your app (e.g. for Apache: BrowserMatch “iOS 8\.” nokeepalive in the mod file setenvif.conf)
If you don’t have access to the server, you can try sending your requests with a Connection: close header: this will tell the server to drop the connection immediately and to respond without any keep alive headers. BUT at the moment, NSURLSession seems to override the Connection header when the requests are sent (we didn’t test this solution extensively as we can tweak the Apache configuration)
shareimprove this answer
edited Oct 26 at 15:56

Jon
30.1k1898129
answered Sep 23 ’14 at 13:54

Arthur
1,3941310
4
Here is an exemple project demonstrating the issue, a bug report have been submitted to Apple too. cl.ly/Xgkl/keep-alive-fail.zip Launch the project, click on the first post button (top on the screen), wait for 5 seconds, click on it again, error. – Dimillian77 Sep 23 ’14 at 15:25
5
Keep-alive is bilateral. Clients will add an http header “Connection:Keep-Alive” by default, adding keep-alive parameters on the client requests may help; e.g. “Keep-Alive:max=1”. Arthur’s comment is very helpful but also indicates more than 1 problem in iOS8 simulator networking. I have to use https to get a connection as http fails before sending a request. – ptc Sep 24 ’14 at 23:54
2
Hey guys, I’m having this exact same problem on the device. Is there a fix for this? There is no problem on iOS 7 though. – Andres C Oct 9 ’14 at 14:08
2
Tip: you can use the NSURLErrorNetworkConnectionLost constant instead of hard-coding -1005. – Vincent Tourraine Apr 9 at 9:47
7
The issue persists in iOS 9 public beta 3. – Ken Van Hoeylandt Aug 25 at 9:28

The link: http://stackoverflow.com/questions/25372318/error-domain-nsurlerrordomain-code-1005-the-network-connection-was-lost[:]

Print Friendly, PDF & Email
Scroll to Top