导入第三方Reachablility
v.m导入头文件
#import "Reachability.h"
创建网络检测属性
@property(nonatomic,strong)Reachability *reachli;
- (void)viewDidLoad {
[super viewDidLoad];
//创建网络检测对象
self.reachli = [Reachability reachabilityForInternetConnection];
//创建通知
//kReachabilityChangedNotification网络状态改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:kReachabilityChangedNotification object:nil];
//开始监听网络状态
[self.reachli startNotifier];
[self change];
}
//接收到通知调用的方法
-(void)change{
//网络连接状态
NetworkStatus status = [self.reachli currentReachabilityStatus];
//swith 循环检测网络状态
switch (status) {
case NotReachable:{
NSLog(@"没有网络");
UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"网络提示" message:@"没有网络连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[aler show];
break;
}
case ReachableViaWiFi:{
NSLog(@"连接到WIFi");
UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"网络提示" message:@"WIFI在线" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[aler show];
break;
}
case ReachableViaWWAN:{
NSLog(@"连接到蜂窝");
UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"网络提示" message:@"3/4/5G在线" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[aler show];
break;
}
default:
break;
}
}
- (void)dealloc{
//停止网络监听
[self.reachli stopNotifier];
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}