Why NSURLConnection failed to complete with Error Domain = NSURLErrorDomain Code = -1005 "Network connection was lost." in Swift iOS8?

I am using Xcode beta6. I created an application that has the 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 gets the correct JSON result when the server is on the network with a LAN cable, but when this server is 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 this is really strange, because if I insert the json path into the browser, I see json. So only on iOS devices I canโ€™t cope, but I donโ€™t know what I have to fix. Can anybody help me?

So, if my Mac mini, which I use for development, is on Lan, and the server on Lan, everything works fine. But when my Mac mini is on WiFi and my server is on Wi-Fi, I get this error ...

+8
ios swift nsurlconnection nsurl nsurlconnectiondelegate
source share
2 answers

Well, my first question is: did you try to accomplish the same task using the same logic / code in Objective-C / iOS 7? This would give us an idea if this is a problem in Swift, iOS 8 or a problem in your code. If you have, submit this code in the editor / update.

Second question: why are you overriding accept-encoding? Many servers require something there if you specify a header value. It is best to remove this.

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

therefore, it may be a bug in the beta versions of iOS 8. First I will try Objective-C, and if it also breaks, go to the developer forums and post your problem to attract Apple's attention to it. You can also open a radar for him.

+2
source share

I completely solved this problem by deleting my Wi-Fi network connection and re-creating it, selecting it from the list of network connections and again providing the password. See My stack overflow answer at the bottom of the page:

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

0
source share

All Articles