Reachability网络情况之详解


Reachability判断网络情况

// 把Reachability声明成属性

@property (strong , nonatomic ) Reachability *reach;


//网络监听实现
-(void)setNetNotice
{

     //开启网络状况监听 测试服务器状态
    self.reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    
    [self.reach startNotifier];//开始监听.会启动一个run loop  kReachabilityChangedNotification
    

}


//通知
- (void)reachabilityChanged:(NSNotification *)note{

   self.reach = [note object];

    NSParameterAssert([self.reach isKindOfClass:[Reachability class]]);

    NetworkStatus status = [self.reach currentReachabilityStatus];

    // 网络没有连接时
    if(status == NotReachable){

     // 弹出提示框

        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:nil message:@"网络已断开" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];

        [alterView show];
     
    }else if(status == ReachableViaWWAN){ // 使用流量时

        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:nil message:@"当前为2G/3G网络,请注意流量" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];

        [alterView show];
     
    }else if(status == ReachableViaWiFi){ // 链接wifi时

        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:nil message:@"当前为WiFi网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];

        [alterView show];
     
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值