一般有两种方式,都是第三方的框架,轮子嘛,能用就先用着,后面再优化。
一:Reachability
1.首先在AppDelegate.h添加头文件"Reachability.h",导入框架SystemConfiguration.frame。
2. 在AppDelegate.m中这样实现:
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
2 //开启网络状况的监听
3 //来订阅实时的网络状态变化通知。导入Reachability.h头文件,然后注册一个对象来订阅网络状态变化的信息,网络状态变化的信息名称为kReachabilityChanged-Notification
4
5 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
6 //通过检查某个主机能否访问来判断当前网络是否可用:
7 self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
8
9 //开始监听,会启动一个run loop
10 [self.hostReach startNotifier];
11
12 }
13
14 -(void)reachabilityChanged:(NSNotification *)note{
15 Reachability *currReach = [note object];
16
17 NSParameterAssert([currReach isKindOfClass:[Reachability class]]);
18
19 //对连接改变做出响应处理动作
20
21 NetworkStatus status = [currReach currentReachabilityStatus];
22
23 //如果没有连接到网络就弹出提醒实况
24
25 self.isReachable = YES;
26
27 if(status == NotReachable){
28
29 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
30 [alert show];
31 [alert release];
32 self.isReachable = NO;
33 return;
34
35 }
36
37 if (status==kReachableViaWiFi||status==kReachableViaWWAN) {
38
39 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
40
41 // [alert show];
42
43 [alert release];
44 self.isReachable = YES;
45
46 }
47 }
48
49 然后在每个页面的viewWillAppear:加上:
50
51 -(void)viewWillAppear:(BOOL)animated{
52 [super viewWillAppear:YES];
53 AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
54 if(appDlg.isReachable){
55 NSLog(@"网络已连接");//执行网络正常时的代码
56 }
57 else{
58 NSLog(@"网络连接异常");//执行网络异常时的代码
59 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
60 [alert show];
61 [alert release];
62 }
63 }
这样就可以检查到在运行程序时网络突然的中断和连接。Reachability类实际上是苹果公司对SCNetworkReachability API的封装,这个API定义在SystemConfigure.framework库中。如果有其他特别的需求,也可以直接使用这个原生的SCNetworkReachability类。
二:AFNetworking监测
1.导入框架,和头文件#import <AFNetworkReachabilityManager.h>
2.代码:
1 -(void)afn{
2 //1.创建网络状态监测管理者
3 AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
4 //开启监听,记得开启,不然不走block
5 [manger startMonitoring];
6 //2.监听改变
7 [manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
8 /*
9 AFNetworkReachabilityStatusUnknown = -1,
10 AFNetworkReachabilityStatusNotReachable = 0,
11 AFNetworkReachabilityStatusReachableViaWWAN = 1,
12 AFNetworkReachabilityStatusReachableViaWiFi = 2,
13 */
14 switch (status) {
15 case AFNetworkReachabilityStatusUnknown:
16 NSLog(@"未知");
17 break;
18 case AFNetworkReachabilityStatusNotReachable:
19 NSLog(@"没有网络");
20 break;
21 case AFNetworkReachabilityStatusReachableViaWWAN:
22 NSLog(@"3G|4G");
23 break;
24 case AFNetworkReachabilityStatusReachableViaWiFi:
25 NSLog(@"WiFi");
26 break;
27 default:
28 break;
29 }
30 }];
31 }
来源:https://www.cnblogs.com/somethingWithiOS/p/5948606.html