![]()
![]()
![]()
1 - (void)viewDidLoad
2 {
3 [super viewDidLoad];
4
5 // 监听网络状态发生改变的通知
6 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
7
8 // 获得Reachability对象
9 self.reachability = [Reachability reachabilityForInternetConnection];
10 // 开始监控网络
11 [self.reachability startNotifier];
12
13 // // 1.获得Reachability对象
14 // Reachability *wifi = [Reachability reachabilityForLocalWiFi];
15 //
16 // // 2.获得Reachability对象的当前网络状态
17 // NetworkStatus wifiStatus = wifi.currentReachabilityStatus;
18 // if (wifiStatus != NotReachable) {
19 // NSLog(@"是WIFI");
20 // }
21 }
22
23 - (void)dealloc
24 {
25 [self.reachability stopNotifier];
26 [[NSNotificationCenter defaultCenter] removeObserver:self];
27 }
28
29 - (void)networkStateChange
30 {
31 NSLog(@"网络状态改变了");
32 [self checkNetworkState];
33 }
34
35 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
36 {
37 [self checkNetworkState];
38 }
39
40 /**
41 * 监测网络状态
42 */
43 - (void)checkNetworkState
44 {
45 if ([HMNetworkTool isEnableWIFI]) {
46 NSLog(@"WIFI环境");
47 } else if ([HMNetworkTool isEnable3G]) {
48 NSLog(@"手机自带网络");
49 } else {
50 NSLog(@"没有网络");
51 }
52 }