Handle No Internet Connection Error Before Try to Parse the Result in Alamofire

后端 未结 7 1895
旧巷少年郎
旧巷少年郎 2020-12-30 03:04

How should I handle if there is an error occurs when there is no internet connection in Alamofire. I tried checking if data is nil or not but it does not work.

Below

7条回答
  •  梦毁少年i
    2020-12-30 03:53

    I agree with @Shripada. First you should use Reachability to check for connectivity. There is a Swift library here: https://github.com/ashleymills/Reachability.swift

    additionally you can use one of the Alamofire validation methods:

    Alamofire.request(.POST, REGISTER_URL, parameters: parameters, encoding: .JSON, headers: getAuthenticationHeader()).validate(statusCode: 200 ..< 300).response { (request, response, data, error) -> Void in
        if error != nil {
            println("Server responded with: \(response.statusCode)")
            return
        }
    
        // Handle your response data here
    }
    

提交回复
热议问题