官方Demo下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
将Reachability.h 和 Reachability.m 加到自己的项目中,并引用 SystemConfiguration.framework,就可以使用了。
下面代码:
// // ViewController.m // 网络状态监测 // // Created by 王卫亮 on 15/2/4. // Copyright © 2015年 王卫亮. All rights reserved. // #import "ViewController.h" #import "Reachability.h" #import <ifaddrs.h> @interface ViewController () @property (nonatomic, strong)Reachability *conn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 设置网络状态变化时的通知函数 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil]; self.conn = [Reachability reachabilityForInternetConnection]; [self.conn startNotifier];//开始监测 } -(void)dealloc{ [self.conn stopNotifier];//停止监测 [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)networkStateChange{ [self checkNetworkState]; } //检测网络状态 - (void)checkNetworkState{ // 1.检测wifi状态 Reachability *wifi = [Reachability reachabilityForLocalWiFi]; // 2.检测手机是否能上网络(WIFI3G2.5G) Reachability *conn = [Reachability reachabilityForInternetConnection]; // 3.判断网络状态 if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi NSLog(@"有wifi"); } else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网 NSLog(@"使用手机自带网络进行上网"); } else { // 没有网络 NSLog(@"没有网络"); } } //如果只是判断是否有网,可以返回BOOL值 - (BOOL)checkNetwork{ // 1.检测wifi状态 Reachability *wifi = [Reachability reachabilityForLocalWiFi]; // 2.检测手机是否能上网络(WIFI3G2.5G) Reachability *conn = [Reachability reachabilityForInternetConnection]; // 3.判断网络状态 if ([wifi currentReachabilityStatus] != NotReachable || [conn currentReachabilityStatus] != NotReachable) { return YES; } else { // 没有网络 return NO; } } //需要导入ifadds头文件 //是否连接VPN - (BOOL)isVPNConnected{ struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; //检索当前接口,0 成功 success = getifaddrs(&interfaces); if (success == 0) { //循环链表接口 temp_addr = interfaces; while (temp_addr != NULL) { NSString *string = [NSString stringWithFormat:@"%s" , temp_addr->ifa_name]; if ([string rangeOfString:@"tap"].location != NSNotFound || [string rangeOfString:@"tun"].location != NSNotFound || [string rangeOfString:@"ppp"].location != NSNotFound){ return YES; } temp_addr = temp_addr->ifa_next; } } // Free memory freeifaddrs(interfaces); return NO; } @end
很多公司的项目中都使用了AFNetworking, 或者封装了AFNetworking的YTKNetworking ,AFNetworking封装有Reachability的网络监测,可以直接拿来用
#pragma 监测网络的可链接性 + (BOOL)netWorkReachabilityWithURLString:(NSString *) strUrl { __block BOOL netState = NO; NSURL *baseURL = [NSURL URLWithString:strUrl]; AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL]; NSOperationQueue *operationQueue = manager.operationQueue; [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusReachableViaWWAN: case AFNetworkReachabilityStatusReachableViaWiFi: [operationQueue setSuspended:NO]; netState = YES; break; case AFNetworkReachabilityStatusNotReachable: netState = NO; default: [operationQueue setSuspended:YES]; break; } }]; [manager.reachabilityManager startMonitoring]; return netState; }