在 iOS 上检测互联网连接的最简单方法? [英] Easiest way to detect Internet connection on iOS?

查看:32
本文介绍了在 iOS 上检测互联网连接的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题会被许多其他问题所欺骗,但是,我觉得这里没有很好地解释这个简单的案例.来自 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.

我知道 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's:不要忘记将 SystemConfiguration.framework 添加到您的项目中.

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

这篇关于在 iOS 上检测互联网连接的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆