本文介绍了在尝试解析Alamofire中的结果之前处理无Internet连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果在Alamofire中没有互联网连接时发生错误,我应该如何处理。我试过检查数据是否为零但不起作用。
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.
以下是我如何使用Alamofire
Below is how I use Alamofire
Alamofire.request(.POST, REGISTER_URL, parameters: parameters, encoding: .JSON, headers: getAuthenticationHeader()).response { (request, response, data, errorType) -> Void in let resultObject: APIResults = APIResults(JSONDecoder(data!)); let responseCode: Int = Int(resultObject.code!)!;// THIS CRASHES WHEN THERE IS NO INTERNET CONNECTION if (responseCode == 200) { available = true; } finished = true; }推荐答案
我同意与@Shripada。首先,您应该使用Reachability检查连接性。 这里有一个Swift库: github/ashleymills/Reachability.swift
I agree with @Shripada. First you should use Reachability to check for connectivity. There is a Swift library here: github/ashleymills/Reachability.swift
此外,您可以使用其中一种Alamofire验证方法:
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 }更多推荐
在尝试解析Alamofire中的结果之前处理无Internet连接错误
发布评论