iOS中使用 Reachability 检测网络

如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote)。那么你会使用到Reachability来实现网络检测。

写本文的目的
  • 了解Reachability都能做什么
  • 检测3中网络环境
    • 2G/3G
    • wifi
    • 无网络
  • 如何使用通知
    • 单个controller
    • 多个controller
  • 简单的功能:
    • 仅在wifi下使用
Reachability简介
Reachablity 是一个iOS下检测,iOS设备网络环境用的库。
  • 监视目标网络是否可用
  • 监视当前网络的连接方式
  • 监测连接方式的变更
苹果官方提供的Doc

Github上的文档

安装
  • 创建 network 工程(network是我创建的demo工程,附件中可以下载到)
  • 使用Cocoaspod安装依赖
  • 在项目中添加 SystemConfiguration.framework 库
由于Reachability非常常用。直接将其加入到Supporting Files/networ-Prefix.pch中:
  1. #import
复制代码
如果你还不知道cocoaspod是什么,看这里:

使用
stackoverflow上有一篇回答,很好的解释了reachability的用法
  • 一般情况一个Reachability实例就ok了。
  • 一个Controller只需要一个Reachability
Block方式使用
  1. - (void)viewDidLoad
  2. {
  3.     [super viewDidLoad];
  4.      DLog(@"开启 [url]www.apple.com[/url] 的网络检测");
  5.      Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
  6.      DLog(@"-- current status: %@", reach.currentReachabilityString);
  7.     
  8.      // start the notifier which will cause the reachability object to retain itself!
  9.     
  10.      [[NSNotificationCenter defaultCenter] addObserver:self
  11.                                                         selector:@selector(reachabilityChanged:)
  12.                                                              name:kReachabilityChangedNotification
  13.                                                           object:nil];
  14.     
  15.      reach.reachableBlock = ^(Reachability * reachability)
  16.     {
  17.         dispatch_async(dispatch_get_main_queue(), ^{
  18.             self.blockLabel.text = @"网络可用";
  19.                self.blockLabel.backgroundColor = [UIColor greenColor];
  20.         });
  21.     };
  22.    
  23.     reach.unreachableBlock = ^(Reachability * reachability)
  24.     {
  25.         dispatch_async(dispatch_get_main_queue(), ^{
  26.             self.blockLabel.text = @"网络不可用";
  27.                self.blockLabel.backgroundColor = [UIColor redColor];
  28.         });
  29.     };
  30.     
  31.      [reach startNotifier];
  32. }
复制代码
使用notification的方式
  1. - (void)viewDidLoad
  2. {
  3.     [super viewDidLoad];
  4.      DLog(@"开启 [url]www.apple.com[/url] 的网络检测");
  5.      Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
  6.      DLog(@"-- current status: %@", reach.currentReachabilityString);
  7.     
  8.      // start the notifier which will cause the reachability object to retain itself!
  9.     
  10.      [[NSNotificationCenter defaultCenter] addObserver:self
  11.                                                         selector:@selector(reachabilityChanged:)
  12.                                                              name:kReachabilityChangedNotification
  13.                                                           object:nil];
  14.      [reach startNotifier];
  15. }

  16. - (void) reachabilityChanged: (NSNotification*)note {
  17.      Reachability * reach = [note object];
  18.    
  19.     if(![reach isReachable])
  20.     {
  21.         self.notificationLabel.text = @"网络不可用";
  22.           self.notificationLabel.backgroundColor = [UIColor redColor];
  23.           self.wifiOnlyLabel.backgroundColor = [UIColor redColor];
  24.           self.wwanOnlyLabel.backgroundColor = [UIColor redColor];
  25.           return;
  26.     }
  27.        
  28.      self.notificationLabel.text = @"网络可用";
  29.      self.notificationLabel.backgroundColor = [UIColor greenColor];
  30.     
  31.      if (reach.isReachableViaWiFi) {
  32.           self.wifiOnlyLabel.backgroundColor = [UIColor greenColor];
  33.           self.wifiOnlyLabel.text = @"当前通过wifi连接";
  34.      } else {
  35.           self.wifiOnlyLabel.backgroundColor = [UIColor redColor];
  36.           self.wifiOnlyLabel.text = @"wifi未开启,不能用";
  37.      }
  38.     
  39.      if (reach.isReachableViaWWAN) {
  40.           self.wwanOnlyLabel.backgroundColor = [UIColor greenColor];
  41.           self.wwanOnlyLabel.text = @"当前通过2g or 3g连接";
  42.      } else {
  43.           self.wwanOnlyLabel.backgroundColor = [UIColor redColor];
  44.           self.wwanOnlyLabel.text = @"2g or 3g网络未使用";
  45.      }
  46. }
复制代码
附件demo说明 开启wifi状态

关闭wifi的状态
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值