Search code examples
iosswiftnsurlconnection

Swift NSURLConnection returns instantly with a 1005 error


I working on a new app written in swift, I've seen this error message spammed all over SO saying: Restart the simulator.

Too bad it seems that in my case this is not the answer, an older app written in objective-C which seems to be using exactly the same methods does work fine, but when I make NSUrlConnection calls to an Api (ours) it does not seem to work for the new app.

The response is a proper 200 with some json data, no redirects it works fine on the Android app I'm developing simultaneously.

basicly what I have:

func getRequest() -> NSMutableURLRequest {
        let request = NSMutableURLRequest(URL: NSURL(string: package.fullPath)!)
        request.HTTPMethod = package.apiMethod.description;
        request.HTTPBody = "hi".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
        return request;
    }

    func execute() {
        conn = NSURLConnection(request: getRequest(), delegate: self);
        if let c = conn {
            Debug.log("** api-connection-start **");
        }

    }

relevant listeners (the class implements NSURLConnectionDataDelegate):

func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
    Debug.log("** api-recieved-url-response? **")
    let httpResponse = response as NSHTTPURLResponse
    statusCode = httpResponse.statusCode
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
    Debug.log("** api-recieving-data **")
    self.responseData.appendData(data)
}

func connection(connection: NSURLConnection, didFailWithError error: NSError) {
    Debug.log("** api-fail-with-error **")
    ErrorClass.message(error)
    if let responseListener = listener {
        responseListener.onError(package, error:error)
    }
}

I have breakpoints on all those helper methods, only the didFailWith error is instantly called with an error containing:

2015-03-31 16:58:29.853 myappname[537:4136] {
    NSErrorFailingURLKey = "http://xxxxxxxx/valid/api/call";
    NSErrorFailingURLStringKey = "http://xxxxxxxx/valid/api/call";
    NSLocalizedDescription = "The network connection was lost.";
    NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1005 \"The network connection was lost.\" UserInfo=0x7fdf3bc465d0 {NSErrorFailingURLStringKey=http://xxxxxxxx/valid/api/call, NSErrorFailingURLKey=http://xxxxxxxx/valid/api/call, _kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4, NSLocalizedDescription=The network connection was lost.}";
    "_kCFStreamErrorCodeKey" = "-4";
    "_kCFStreamErrorDomainKey" = 4;
}

Obviously I replaced the company name and exact called url since I cannot share these, I can guarantee that these are correct though.

Does anyone see a mistake causing my tissues?


Solution

  • After crying lots of hours till I fell asleep and waking up and returning to work it magically works again.

    Thanks Obama! Anyway, just make sure you clear all data from xcode besides cleaning, this can sadly cause these issues too, not just restarting the simulator.