第三方网络检测

导入第三方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];
    
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值