星空网 > 软件开发 > 操作系统

iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 G

  

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

 

写本文的目的

  • 了解Reachability都能做什么
  • 检测3中网络环境
    • 2G/3G
    • wifi
    • 无网络
  • 如何使用通知
    • 单个controller
    • 多个controller
  • 简单的功能:
    • 仅在wifi下使用

Reachability简介

Reachablity 是一个iOS下检测,iOS设备网络环境用的库。

  • 监视目标网络是否可用
  • 监视当前网络的连接方式
  • 监测连接方式的变更

苹果官方提供的Doc

http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

 

Github上的文档

https://github.com/tonymillion/Reachability

 

安装

  1. 创建 network 工程(network是我创建的demo工程,附件中可以下载到)
  2.  使用Cocoaspod安装依赖
  3. 在项目中添加 SystemConfiguration.framework 库

由于Reachability非常常用。直接将其加入到Supporting Files/networ-Prefix.pch中:

 

C代码  iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 Gimages/loading.gif' data-original="http://witcheryne.iteye.com/images/icon_star.png" />

  1. #import <Reachability/Reachability.h>  

 

 如果你还不知道cocoaspod是什么,看这里:

http://witcheryne.iteye.com/blog/1873221

 

使用

stackoverflow上有一篇回答,很好的解释了reachability的用法

http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability

  • 一般情况一个Reachability实例就ok了。
  • 一个Controller只需要一个Reachability

Block方式使用

 

C代码  iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 G

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.      DLog(@"开启 www.apple.com 的网络检测");  
  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. }  
  33.    

 

 

使用notification的方式

 

C代码  iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 G

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.      DLog(@"开启 www.apple.com 的网络检测");  
  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.   
  17. - (void) reachabilityChanged: (NSNotification*)note {  
  18.      Reachability * reach = [note object];  
  19.      
  20.     if(![reach isReachable])  
  21.     {  
  22.         self.notificationLabel.text = @"网络不可用";  
  23.           self.notificationLabel.backgroundColor = [UIColor redColor];  
  24.           self.wifiOnlyLabel.backgroundColor = [UIColor redColor];  
  25.           self.wwanOnlyLabel.backgroundColor = [UIColor redColor];  
  26.           return;  
  27.     }  
  28.          
  29.      self.notificationLabel.text = @"网络可用";  
  30.      self.notificationLabel.backgroundColor = [UIColor greenColor];  
  31.       
  32.      if (reach.isReachableViaWiFi) {  
  33.           self.wifiOnlyLabel.backgroundColor = [UIColor greenColor];  
  34.           self.wifiOnlyLabel.text = @"当前通过wifi连接";  
  35.      } else {  
  36.           self.wifiOnlyLabel.backgroundColor = [UIColor redColor];  
  37.           self.wifiOnlyLabel.text = @"wifi未开启,不能用";  
  38.      }  
  39.       
  40.      if (reach.isReachableViaWWAN) {  
  41.           self.wwanOnlyLabel.backgroundColor = [UIColor greenColor];  
  42.           self.wwanOnlyLabel.text = @"当前通过2g or 3g连接";  
  43.      } else {  
  44.           self.wwanOnlyLabel.backgroundColor = [UIColor redColor];  
  45.           self.wwanOnlyLabel.text = @"2g or 3g网络未使用";  
  46.      }  
  47. }  

 

 

附件demo说明

开启wifi状态

iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 G


 关闭wifi的状态

iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 G
 

 

 

遗留问题

    1. 如何在多个controller之前共用一个Reachability(附件demo中是一个controller一个Reachability实例)
    2. 应该在什么使用停止Reachability的检测.



原标题:iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 G

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流