iOS开发 -------- 网络状态监测

一 示例代码


 

需要先把第三方Reachability下载导入到工程中  下载网址  https://github.com/tonymillion/Reachability

1 封装网络工具类 NetworkTool

 1 //
 2 //  NetworkTool.h
 3 //  网络状态检测
 4 //
 5 //  Created by lovestarfish on 15/11/7.
 6 //  Copyright © 2015年 S&G. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface NetworkTool : NSObject
12 
13 /**
14  *  是否WIFI
15  */
16 + (BOOL)IsEnableWIFI;
17 
18 /**
19  *  是否3G
20  */
21 + (BOOL)IsEnable3G;
22 
23 @end

 

 1 //
 2 //  NetworkTool.m
 3 //  网络状态检测
 4 //
 5 //  Created by lovestarfish on 15/11/7.
 6 //  Copyright © 2015年 S&G. All rights reserved.
 7 //
 8 
 9 #import "NetworkTool.h"
10 #import "Reachability.h"
11 
12 @implementation NetworkTool
13 //是否WIFI
14 + (BOOL)IsEnableWIFI {
15     return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
16 }
17 
18 //是否3G
19 + (BOOL)IsEnable3G {
20     return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
21 }
22 
23 @end

 

2 在viewController类中导入我们封装好的网络工具类

 

 1 //
 2 //  RootViewController.m
 3 //  网络状态检测
 4 //
 5 //  Created by lovestarfish on 15/11/7.
 6 //  Copyright © 2015年 S&G. All rights reserved.
 7 //
 8 
 9 #import "RootViewController.h"
10 #import "Reachability.h"
11 #import "NetworkTool.h"
12 
13 @interface RootViewController ()
14 
15 @property (nonatomic,strong) Reachability *reachability;
16 
17 @end
18 
19 @implementation RootViewController
20 
21 - (void)viewDidLoad {
22     [super viewDidLoad];
23 
24     //监听网络状态发生改变的通知
25     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChanged) name:kReachabilityChangedNotification object:nil];
26     
27     //获得Reachability对象
28     self.reachability = [Reachability reachabilityForInternetConnection];
29     
30     //开始监控网络
31     [self.reachability startNotifier];
32 }
33 
34 /**
35  *  网络状态发生改变时
36  */
37 - (void)networkStateChanged {
38     [self checkNetworkState];
39 }
40 
41 /**
42  *  检测当前网络状态
43  */
44 - (void)checkNetworkState {
45     if ([NetworkTool IsEnable3G]) {
46         [self presentAlertController:@"已经连接WIFI"];
47         NSLog(@"WIFi环境");
48     } else if ([NetworkTool IsEnable3G] ) {
49         [self presentAlertController:@"当前为手机自带网络"];
50         NSLog(@"手机自带网络");
51     } else {
52         [self presentAlertController:@"网络不可用"];
53         NSLog(@"没有网络");
54     }
55 }
56 
57 /**
58  *  网络发生变化时以弹出框消息提示用户
59  */
60 - (void)presentAlertController:(NSString *)message {
61     UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
62     UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
63         
64     }];
65     [alertC addAction:action];
66     [self presentViewController:alertC animated:YES completion:nil];
67 }
68 
69 /**
70  *  在合适的时机移除通知
71  */
72 - (void)dealloc {
73     [self.reachability stopNotifier];
74     [[NSNotificationCenter defaultCenter] removeObserver:self];
75 }
76 
77 - (void)didReceiveMemoryWarning {
78     [super didReceiveMemoryWarning];
79 }
80 
81 @end

 

 二 实现效果

 


 

 

 

posted @ 2015-11-24 16:14  晨光微  阅读(235)  评论(0编辑  收藏  举报