(一).Reachability
添加源文件及Framework
- - (void)viewDidLoad {
- [super viewDidLoad];
-
-
-
-
-
-
-
-
-
- NSString *urlStr = @"写一个能连接的网址";
-
-
- self.netConnect = [Reachability reachabilityWithHostname:urlStr];
-
- __weak typeof(self) weakSelf = self;
-
-
- self.netConnect.reachableBlock = ^(Reachability * reachability)
- {
-
-
- NSLog(@"connect success :newName = %@",weakSelf.netConnect.currentReachabilityString);
-
-
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"reload data");
- });
- };
-
-
- self.netConnect.unreachableBlock = ^(Reachability * reachability)
- {
-
- NSLog(@"connect error");
-
-
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"to do something");
- });
- };
-
-
- [self.netConnect startNotifier];
-
-
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(reachabilityChanged:)
- name:kReachabilityChangedNotification
- object:self.netConnect];
-
- }
- - (void)reachabilityChanged:(NSNotification *)notification
- {
-
-
-
-
-
-
-
-
-
- Reachability *reach = notification.object;
-
- switch (reach.currentReachabilityStatus)
- {
- case NotReachable:
- NSLog(@"connect error");
- break;
- case ReachableViaWiFi:
- NSLog(@"connect wifi");
- break;
- case ReachableViaWWAN:
- NSLog(@"connect wwan");
- break;
- default:
- break;
- }
- }
// 监听网络状态改变的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
// 创建Reachability
self.conn = [Reachability reachabilityForInternetConnection];
// 开始监控网络(一旦网络状态发生改变, 就会发出通知kReachabilityChangedNotification) [self.conn startNotifier]; // 处理网络状态改变 - (void)networkStateChange { // 1.检测wifi状态 Reachability *wifi = [Reachability reachabilityForLocalWiFi]; // 2.检测手机是否能上网络(WIFI\3G\2.5G) Reachability *conn = [Reachability reachabilityForInternetConnection]; // 3.判断网络状态 if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi NSLog(@"有wifi"); } else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网 NSLog(@"使用手机自带网络进行上网"); } else { // 没有网络 NSLog(@"没有网络"); } } (二).AFN // 1.获得网络监控的管理者 AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager]; // 2.设置网络状态改变后的处理 [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { // 当网络状态改变了, 就会调用这个block switch (status) { case AFNetworkReachabilityStatusUnknown: // 未知网络 NSLog(@"未知网络"); break; case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网) NSLog(@"没有网络(断网)"); break; case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络 NSLog(@"手机自带网络"); break; case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI NSLog(@"WIFI"); break; } }]; // 3.开始监控 [mgr startMonitoring]; (三)从状态栏中获取网络类型,代码如下: - (NSString *)getNetWorkStates{ UIApplication *app = [UIApplication sharedApplication]; NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews]; NSString *state = [[NSString alloc]init]; int netType = 0; //获取到网络返回码 for (id child in children) { if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) { //获取到状态栏 netType = [[child valueForKeyPath:@"dataNetworkType"]intValue]; switch (netType) { case 0: state = @"无网络"; //无网模式 break; case 1: state = @"2G"; break; case 2: state = @"3G"; break; case 3: state = @"4G"; break; case 5: { state = @"wifi"; break; default: break; } } } //根据状态选择 } return state; } 基本原理是从UIApplication类型中通过valueForKey获取内部属性 statusBar。然后筛选一个内部类型 (UIStatusBarDataNetworkItemView),最后返回他的 dataNetworkType属性,根据状态栏获取网络 状态,可以区分2G、3G、4G、WIFI,系统的方法,比较快捷,不好的是万一连接的WIFI 没有联网的话, 识别不到。