Why NSURLConnection failed with Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." in Swift iOS8?
Asked Answered
R

2

9

I use Xcode beta6. I created an app which have a Downloader class, and this is the Downloader class:

class Downloader : NSObject {

    private var _connection : NSURLConnection?
    private var _downloadedData: NSMutableData?

    func getDataFromURLString(urlToRequest: String!, aType: DownloadedDataType) {

        _downloadedData = NSMutableData()

        var request : NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlToRequest), cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 20.0)
        request.setValue("", forHTTPHeaderField: "Accept-Encoding")

        self._connection = NSURLConnection(request: request, delegate:self)
    }

    func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        println("Data expected size: \(response.expectedContentLength)")
    }

    func connectionDidFinishLoading(connection: NSURLConnection!) {
        println("finished")
    }

    func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
        println("error: \(error)")
    }

    func connection(connection: NSURLConnection!, didReceiveData data: NSData!)  {
        _downloadedData?.appendData(data)
    }

}

This class works well and get the right JSON result when the server is on the network with LAN cable but when this server connected to the same network via WiFi I get this error from the iOS device:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

But Its really weird because if I paste the json path to the browser I see the json.. So only on iOS devices cant handle, but I dont know what I should fix.. Can anyone help me?

So If my Mac mini what I use to develop is on Lan, and the Server is on Lan, everything works fine. But when my Mac mini is on WiFi and my server is on Wifi I get this error...

Raseda answered 3/9, 2014 at 19:2 Comment(2)
Quit simulator and re-run your project.Elrod
Possible duplicate of NSURLConnection GET request returns -1005, "the network connection was lost"Banwell
D
2

Well, my first question is have you tried accomplishing the same task using the same logic/code in Objective-C / iOS 7? This would give us an idea if it's a problem in Swift, iOS 8, or a problem in your code. If you have, please post this code in an edit/update.

Second question: Why are you overriding the accept-encoding? Many servers require something there if you specify the header value. Best to remove that.

Third question: What version of iOS 8 beta are you running? FWIW, a simple search of SO showed this question: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

so it could be a bug in iOS 8 betas. I'd suggest trying objective-c first, and if that also breaks, goto the developer forums and post your issue to get Apple's attention on it. You might also want to open a Radar for it.

Durance answered 3/9, 2014 at 19:18 Comment(4)
Is there a fix? I'm experiencing the same issue using iOS8 (first public release) and Objective-C but only on my home WiFi. The error doesn't occur at my office or other WiFi networks I try. When I curl the endpoint it works fine, so it's definitely a fringe issue with NSURLConnectionPastern
That's a different set of circumstances, so probably warrants a new question (feel free to link with appropriate logs and I'll take a look.) That being said, have you seen the later answers on my answer's link? There appears to be some weirdness with HTTP Keep-alive on iOS 8's NSURLConnection. Perhaps that's doing it?Durance
Ahh, I completely missed your link. It's definitely the same keep-alive issue. Thanks.Pastern
I am facing a problem when Hit API of GET type in didfinshluanchingwithoptions method in app delegate. But same API hit by any other view controller it working fine. Error is "The network connection was lost" every timeGonium
F
0

I completely solved this problem by deleting my Wi-Fi network connection and re-instantiating it by choosing it from the network connection list and providing the password again. See my stack overflow response here at the bottom of the page:

NSURLConnection GET request returns -1005, "the network connection was lost"

Falcon answered 13/6, 2015 at 18:6 Comment(1)
If this question can be answered by your other Stack Overflow post, then you should be marking this question as a duplicate rather than posting an answer...Familiar

© 2022 - 2024 — McMap. All rights reserved.