RealReachability实时监听网络状态

RealReachability优势:

离线模式对网络连接状态的要求比较苛刻,且实际场景经常会遇到“伪连接”的情况苹果提供的Reachability面对此场景力不从心。多方研究后引入了ping能力(此方案流量开销最小,也最简单),实现了简单的实际网络连接监测;后面经过提炼和优化,就有了这个框架。可以告诉大家的是,这个框架在appstore上架应用中已经经受了考验,且经过了长时间的测试,可以放心使用。

在viewDidLoad 中添加通知

- (void)viewDidLoad {

[super viewDidLoad];

//提示信息

self.view.backgroundColor = [UIColor purpleColor];

 UILabel *label= [[UILabel alloc] init];

label.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width);

label.text = @"请切换网络状态,以查看RealReachability对网络状态的实时监控";

label.font = [UIFont boldSystemFontOfSize:17];

label.textAlignment = NSTextAlignmentCenter;

 label.numberOfLines = 0;

 [label  sizeToFit];

label.center = self.view.center;

[self.view addSubview:label];

//添加一个通知监听网络状态切换

[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(networkChanged:)                                            name:kRealReachabilityChangedNotification  object:nil];

[GLobalRealReachability startNotifier];

}

网络改变通知

- (void)networkChanged:(NSNotification *)notification{

RealReachability *reachability= (RealReachability *)notification.object;

ReachabilityStatusstatus= [reachability currentReachabilityStatus];

//    ReachabilityStatuspreviousStatus= [reachability previousReachabilityStatus];

if(status== RealStatusNotReachable){  

  [self showNotificationMessageWithStatus:@"当前无联网连接"];

}

if(status== RealStatusViaWiFi){    

[self showNotificationMessageWithStatus:@"已连接至WiFi"];

}

WWANAccessTypeaccessType= [GLobalRealReachability currentWWANtype];

if(status== RealStatusViaWWAN){

if(accessType== WWANType2G)    { 

       [self showNotificationMessageWithStatus:@"已连接2G"];   

 }else if(accessType== WWANType3G)    {    

    [self showNotificationMessageWithStatus:@"已连接3G"];   

 }else if(accessType== WWANType4G)    {    

    [self showNotificationMessageWithStatus:@"已连接4G"];  

  }else{     

   [self showNotificationMessageWithStatus:@"未知网络"];    

}

}

}

状态切换后提示信息

- (void)showNotificationMessageWithStatus:(NSString *)status{

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"网络状态改变"message:statusdelegate:selfcancelButtonTitle:nilotherButtonTitles:nil, nil];

// 显示 UIAlertView[alert show];

// 添加延迟时间为 2 秒 然后执行 dismiss: 方法

[selfperformSelector:@selector(dismiss:)withObject:alertafterDelay:2];

}

实现dismiss方法

//实现dismiss方法

-(void)dismiss:(UIAlertView*)alert{

// 此处即相当于点击了 cancel 按钮

[alert dismissWithClickedButtonIndex:[alert cancelButtonIndex]animated:YES];

}

//销毁通知

-(void)dealloc{[GLobalRealReachability stopNotifier];[[NSNotificationCenter defaultCenter]removeObserver:self];

}

RealReachability的使用心得

//关于reachability的优化版本

一. 关于目前reachability的缺点

1.现在很流行的公用wifi,需要网页鉴权,鉴权之前无法上网,但本地连接已经建立;

2.存在了本地网络连接,但信号很差,实际无法连接到服务器;

3.iOS连接的路由设备本身没有连接外网。

原因: Reachability相关的框架在底层都是通过SCNetworkReachability来实现网络检测的,所以无法检测实际网络连接情况。

二. 优化方法(RealReachability)

链接: http://www.cocoachina.com/ios/20160224/15407.html1.

集成: pod'RealReachability'2.常用代码:

* 实时监控网络连接状态的方法:

[GLobalRealReachability startNotifier];

[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(networkChanged:) name:@"kRealReachabilityChangedNotification"object:nil];

- (void)networkChanged:(NSNotification *)notification

{

RealReachability *reachability = (RealReachability *)notification.object;

ReachabilityStatus status = [reachability currentReachabilityStatus];if(status == NotReachable) {self.curStatusLabel.text = @"没有网络";

}if(status == ReachableViaWiFi) {self.curStatusLabel.text = @"WiFi网络";

}if(status == ReachableViaWWAN) {self.curStatusLabel.text = @"3/4G网络";

}

}

* 手动检测网络的方法

[GLobalRealReachability startNotifier];

- (IBAction)checkTheStatus:(id)sender {

[GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) {switch(status)

{caseNotReachable:

{

[[[UIAlertView alloc] initWithTitle:@"提示"message:@"没有网络"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil, nil] show];break;

}caseReachableViaWiFi:

{

[[[UIAlertView alloc] initWithTitle:@"提示"message:@"WIFI"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil, nil] show];break;

}caseReachableViaWWAN:

{

[[[UIAlertView alloc] initWithTitle:@"提示"message:@"3G/4G"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil, nil] show];break;

}default:break;

}

}];

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值