在尝试解析Alamofire中的结果之前处理无Internet连接错误

编程入门 行业动态 更新时间:2024-10-27 05:29:15
本文介绍了在尝试解析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连接错误

本文发布于:2023-11-26 15:11:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Alamofire   Internet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!