Reachability.h 开源类的使用:判断网络环境,连接情况(无网络连接,3G,WIFI,GPRS)
2014-03-28 17:10
811 查看
在ASIHttpRequest开源库中有这个类。
先看Reachability.h发现
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
所以如果我们在项目中需要用到此类的话,需要引入SystemConfiguration.framework。
此类在ios网络开发中可以确认判断网络环境,连接情况(无网络连接,3G,WIFI,GPRS)
enum { // DDG NetworkStatus Constant Names.
kNotReachable = 0, // Apple's code depends upon 'NotReachable' being the same value as 'NO'.
kReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.
kReachableViaWiFi
};
定义三种网络类型:
一:kNotReachable 无网络连接
二:kReachableViaWWAN 使用GPRS或者3G网络连接
三:kReachableViaWiFi 使用WIFI连接
我在项目中用下面的语句判断是否存在网络连接
BOOL reachable = [[Reachability reachabilityForInternetConnection] isReachable];
if (!reachable) {
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"该功能需要连接网络才能使用,请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease];
[alertView show];
return;
}
当有网络请求的时候,类中方法可以返回目前的网络连接状态
例如:Reachable *reachable = [Reachable reachabilityWithHostName:@"http://blog.csdn.net"];
之后我们应用
// These are the status tests.
- (NetworkStatus) currentReachabilityStatus; 返回网络连接状态
switch( [reachable currentReachabilityStatus ] ) {
/* [reachable currentReachabilityStatus ]包含三个值
一:kNotReachable 无网络连接
二:kReachableViaWWAN 使用GPRS或者3G网络连接
三:kReachableViaWiFi 使用WIFI连接 */
}
先看Reachability.h发现
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
所以如果我们在项目中需要用到此类的话,需要引入SystemConfiguration.framework。
此类在ios网络开发中可以确认判断网络环境,连接情况(无网络连接,3G,WIFI,GPRS)
enum { // DDG NetworkStatus Constant Names.
kNotReachable = 0, // Apple's code depends upon 'NotReachable' being the same value as 'NO'.
kReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.
kReachableViaWiFi
};
定义三种网络类型:
一:kNotReachable 无网络连接
二:kReachableViaWWAN 使用GPRS或者3G网络连接
三:kReachableViaWiFi 使用WIFI连接
我在项目中用下面的语句判断是否存在网络连接
BOOL reachable = [[Reachability reachabilityForInternetConnection] isReachable];
if (!reachable) {
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"该功能需要连接网络才能使用,请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease];
[alertView show];
return;
}
当有网络请求的时候,类中方法可以返回目前的网络连接状态
例如:Reachable *reachable = [Reachable reachabilityWithHostName:@"http://blog.csdn.net"];
之后我们应用
// These are the status tests.
- (NetworkStatus) currentReachabilityStatus; 返回网络连接状态
switch( [reachable currentReachabilityStatus ] ) {
/* [reachable currentReachabilityStatus ]包含三个值
一:kNotReachable 无网络连接
二:kReachableViaWWAN 使用GPRS或者3G网络连接
三:kReachableViaWiFi 使用WIFI连接 */
}
相关文章推荐
- android中判断网络连接是否可用 2g、3g、wifi
- 关于在iOS设备上探测WIFI,3G,GPRS使用情况的细节
- 安卓BroadcastReceiver组件使用系列3:使用广播来判断数据网络和无线网络的连接情况
- iphone最牛判断当前手机网络连接类型wifi-2G-3G-4G-5G
- 关于在iOS设备上探测WIFI,3G,GPRS使用情况的细节
- 关于在iOS设备上探测WIFI,3G,GPRS使用情况的细节
- 关于在iOS设备上探测WIFI,3G,GPRS使用情况的细节
- 关于在iOS设备上探测WIFI,3G,GPRS使用情况的细节
- 判断是否连接到网络,如果有网,是3G还是WIFI
- 4.2.2.5 android 判断3G、wifi、2G等网络状态连接
- Android之判断手机连接的网络类型是WIFI还是2G/3G/4G(转载)
- 判断手机使用网络wifi 2G 3G
- 判断手机使用网络wifi 2G 3G
- 关于在iOS设备上探测WIFI,3G,GPRS使用情况的细节
- 关于在iOS设备上探测WIFI,3G,GPRS使用情况的细节 .
- 获取的是开机后WIFI,3G/GPRS网络下使用流量
- 判断网络环境(3G/WIFI)
- 安卓网络连接全解:包括网络连接状态的监听、网络数据使用状态的监听、获取当前网络连接情况、启动wifi、获取当前连接wifi的网络情况、扫描wifi热点