在iOS上检测Internet连接的最简单方法?

编程入门 行业动态 更新时间:2024-10-28 11:23:25
本文介绍了在iOS上检测Internet连接的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道这个问题似乎是许多其他人的骗局,但是,我觉得这个简单的案例在这里没有得到很好的解释。来自Android和BlackBerry背景,如果没有可用的连接,通过 HTTPUrlConnection 发出请求会立即失败。这似乎是完全理智的行为,我很惊讶地发现iOS中的 NSURLConnection 没有模仿它。

I know this question will appear to be a dupe of many others, however, I don't feel the simple case is well explained here. Coming from an Android and BlackBerry background, making requests through HTTPUrlConnection instantly fail if there is no connection available. This seems like completely sane behavior, and I was surprised to find NSURLConnection in iOS did not emulate it.

I了解Apple(以及其他扩展它的人)提供 Reachability 类来帮助确定网络状态。我很高兴第一次看到这一点并完全期望看到像 bool isNetworkAvailable()这样的东西,但令我惊讶的是,我发现了一个需要通知注册和回调的复杂系统,以及一堆看似不必要的细节。必须有更好的方法。

I understand that Apple (and others who have extended it) provide a Reachability class to assist with determining the network state. I was happy to first see this and fully expected to see something like bool isNetworkAvailable(), but instead to my surprise I found a complex system requiring notification registrations and callbacks, and a bunch of seemingly unnecessary details. There must be a better way.

我的应用已经优雅地处理连接失败,包括没有连接。用户会收到失败的通知,然后应用继续运行。

My app already gracefully handles connection failures, including no connectivity. The user is notified of the failure, and the app moves on.

因此我的要求很简单:我可以在所有HTTP请求之前调用单个同步函数来确定是否我应该打扰实际发送请求。理想情况下,它不需要设置,只返回一个布尔值。

Thus my requirements are simple: Single, synchronous function I can call before all HTTP requests to determine if I should bother actually sending the request or not. Ideally it requires no set up and just returns a boolean.

这在iOS上真的不可能吗?

Is this really not possible on iOS?

推荐答案

我做了一些研究,我正在用更新的解决方案更新我的答案。我不确定你是否已经看过它,但Apple提供了一个很好的示例代码。

I did a little more research and I am updating my answer with a more current solution. I am not sure if you have already looked at it but there is a nice sample code provided by Apple.

下载示例代码这里

在项目中包含Reachability.h和Reachability.m文件。看看ReachabilityAppDelegate.m,看看如何通过WiFi,WWAN等确定主机可达性,可达性等的示例。只需检查网络可达性,就可以做到这样的事情

Include the Reachability.h and Reachability.m files in your project. Take a look at ReachabilityAppDelegate.m to see an example on how to determine host reachability, reachability by WiFi, by WWAN etc. For a very simply check of network reachability, you can do something like this

Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; NetworkStatus networkStatus = [networkReachability currentReachabilityStatus]; if (networkStatus == NotReachable) { NSLog(@"There IS NO internet connection"); } else { NSLog(@"There IS internet connection"); }

@ BenjaminPiette:不要忘记将SystemConfiguration.framework添加到项目中。

@BenjaminPiette's: Don't forget to add SystemConfiguration.framework to your project.

更多推荐

在iOS上检测Internet连接的最简单方法?

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

发布评论

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

>www.elefans.com

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