摘要:
1//2//BWNetWorkTool.h3//IOS_0131_检测网络状态4//5//Createdbymacon16/1/31.6//Copyright2016年博文科技.Allrightsreserved.7//89#import1011@interfaceBWNetWorkTool:NSObject12///是否是WiFi13+(BOOL
1 // 2 //BWNetWorkTool.h 3 //IOS_0131_检测网络状态 4 // 5 //Created by ma c on 16/1/31. 6 //Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interfaceBWNetWorkTool : NSObject 12 ///是否是WiFi 13 +(BOOL)isEnableWiFi; 14 ///是否是3G 15 +(BOOL)isEnable3G; 16 17 @end 18 19 // 20 //BWNetWorkTool.m 21 //IOS_0131_检测网络状态 22 // 23 //Created by ma c on 16/1/31. 24 //Copyright © 2016年 博文科技. All rights reserved. 25 // 26 27 #import "BWNetWorkTool.h" 28 #import "Reachability.h" 29 30 @implementationBWNetWorkTool 31 32 //是否是WiFi 33 +(BOOL)isEnableWiFi 34 { 35 return [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] !=NotReachable; 36 } 37 //是否是3G 38 +(BOOL)isEnable3G 39 { 40 return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] !=NotReachable; 41 } 42 43 44 @end
1 // 2 //ViewController.m 3 //IOS_0131_检测网络状态 4 // 5 //Created by ma c on 16/1/31. 6 //Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "Reachability.h" 11 #import "BWNetWorkTool.h" 12 13 @interfaceViewController () 14 15 @property (nonatomic, strong) Reachability *reachability; 16 17 @end 18 19 @implementationViewController 20 /* 21 检测网络状态 22 1.在网络应用中,需要对用户设备的网络状态进行实时监控,目的: 23 a.让用户了解自己的网络状态,防止一些误会(怪应用无能) 24 b.根据用户的网络状态进行智能处理,节省用户流量,提高用户体验 25 WIFI/3G/4G网络:自动下载高清图片 26 低速网络:只能下载缩略图 27 没有网络:智能显示离线的缓存数据 28 c.苹果官方提供了一个叫Reachability的示例程序,便于开发者检测网络状态 29 https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 30 31 2.Reachability的使用步骤 32 1>添加框架SystemConfiguration.framework 33 2>添加源代码 34 3>包含头文件 - #import "Reachability.h" 35 36 3.常见用法 37 1>是否是WiFi 38 + (BOOL)isEnableWiFi 39 { 40 return [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] !=NotReachable; 41 } 42 2>是否是3G 43 + (BOOL)isEnable3G 44 { 45 return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] !=NotReachable; 46 } 47 */ 48 49 - (void)viewDidLoad { 50 [super viewDidLoad]; 51 52 //监听网络状态发生改变通知 53 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil]; 54 55 //获得Reachability对象 56 self.reachability =[Reachability reachabilityForInternetConnection]; 57 58 //开始监控 59 [self.reachability startNotifier]; 60 61 // //获取Reachability对象 62 //Reachability *wifi = [Reachability reachabilityForLocalWiFi]; 63 // //获取Reachability对象的当前网络状态 64 //NetworkStatus wifiStatus = wifi.currentReachabilityStatus; 65 // 66 //if (wifiStatus !=NotReachable) { 67 //NSLog(@"wifi"); 68 //} 69 70 } 71 72 - (void)networkStateChange 73 { 74 NSLog(@"网络状态改变了"); 75 [self changeNetworkState]; 76 } 77 78 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 79 { 80 [self changeNetworkState]; 81 } 82 83 - (void)changeNetworkState 84 { 85 if([BWNetWorkTool isEnableWiFi]) { 86 NSLog(@"WiFi环境"); 87 }else if([BWNetWorkTool isEnable3G]){ 88 NSLog(@"手机自带网络"); 89 }else{ 90 NSLog(@"没有网络"); 91 } 92 } 93 94 - (void)dealloc 95 { 96 [self.reachability stopNotifier]; 97 [[NSNotificationCenter defaultCenter] removeObserver:self]; 98 } 99 100 @end