Alamofire: Request error: NSURLErrorDomain Code -1005 "The network connection was lost."

I’m working on transferring my project from AFNetworking to Alamofire. Really like the project. However, I’m receiving this error when attempting to make a GET request that worked fine with AFNetworking.

Here’s some example code:

class func listCloudCredntials(includePending includePending: Bool = true, completionBlock:((data: [[String: AnyObject]]?, error: NSError?) -> Void)?) {

    let parameters: [String: AnyObject] = includePending ? ["include_pending": "true"] : [:]

    let urlString = https://myapp-staging.herokuapp.com/api/1/credntials

    let token = SSKeychain.passwordForService(keyJWT, account: user.objectId)

    let headers: [String: String] = ["Authorization": "Bearer \(token)"]

    let request = Alamofire.request(.GET, urlString, parameters: parameters, encoding: .JSON, headers: headers)

    request.responseJSON() {
        (response: Response<AnyObject, NSError>) in

        switch response.result {
        case .Success(let result):
            completionBlock?(data: (result as? [String: AnyObject])?["data"] as? [[String: AnyObject]], error: nil)

        case .Failure(let error):
            completionBlock?(data: nil, error: error)
        }
    }
}

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x14e9c77f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://myapp-staging.herokuapp.com/api/1/credntials, NSErrorFailingURLKey=https://myapp-staging.herokuapp.com/api/1/credntials, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}

I’ve tried running on the iOS Simulator with 1OS 8.4 and iOS 9.1, as well as my iPhone 6S running iOS 9.1.

What am I doing wrong?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

@jensonjohn001 Alamofire 5 now has such a retrier built in, so it’s no longer necessary to do it manually. Plus, the built in retriers are more efficient than recursively calling your request method.

Also, you should never, ever, use a reachability precheck. Apple has recommended against it for years and will produce spurious failure for you, especially during network transitions.

For the record, same issue and it’s worked removing .JSON encoding.

@geoffsdavis You need to use URLEncoding instead of JSONEncoding if you want URL encoded parameters.