国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

【開發(fā)記錄】iOS中使用Reachability檢測網(wǎng)絡(luò)

2019-11-14 20:20:24
字體:
供稿:網(wǎng)友

如果你想在iOS程序中提供一僅在wifi網(wǎng)絡(luò)下使用(Reeder),或者在沒有網(wǎng)絡(luò)狀態(tài)下提供離線模式(Evernote)。那么你會(huì)使用到Reachability來實(shí)現(xiàn)網(wǎng)絡(luò)檢測。

 

寫本文的目的

  • 了解Reachability都能做什么
  • 檢測3中網(wǎng)絡(luò)環(huán)境
    • 2G/3G
    • wifi
    • 無網(wǎng)絡(luò)
  • 如何使用通知
    • 單個(gè)controller
    • 多個(gè)controller
  • 簡單的功能:
    • 僅在wifi下使用

Reachability簡介

Reachablity 是一個(gè)iOS下檢測,iOS設(shè)備網(wǎng)絡(luò)環(huán)境用的庫。

  • 監(jiān)視目標(biāo)網(wǎng)絡(luò)是否可用
  • 監(jiān)視當(dāng)前網(wǎng)絡(luò)的連接方式
  • 監(jiān)測連接方式的變更

蘋果官方提供的Doc

http://developer.apple.com/library/ios/

 

Github上的文檔

https://github.com/tonymillion/Reachability

 

安裝

  1. 創(chuàng)建 network 工程(network是我創(chuàng)建的demo工程,附件中可以下載到)
  2.  使用Cocoaspod安裝依賴
  3. 在項(xiàng)目中添加 SystemConfiguration.framework 庫

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

 

C代碼  收藏代碼
  1. #import <Reachability/Reachability.h>  

 

 如果你還不知道cocoaspod是什么,看這里:

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

 

使用

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

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

  • 一般情況一個(gè)Reachability實(shí)例就ok了。
  • 一個(gè)Controller只需要一個(gè)Reachability

Block方式使用

 

C代碼  收藏代碼
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.      DLog(@"開啟 www.apple.com 的網(wǎng)絡(luò)檢測");  
  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 = @"網(wǎng)絡(luò)可用";  
  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 = @"網(wǎng)絡(luò)不可用";  
  27.                self.blockLabel.backgroundColor = [UIColor redColor];  
  28.         });  
  29.     };  
  30.       
  31.      [reach startNotifier];  
  32. }  
  33.    

 

 

使用notification的方式

 

C代碼  收藏代碼
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.      DLog(@"開啟 www.apple.com 的網(wǎng)絡(luò)檢測");  
  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 = @"網(wǎng)絡(luò)不可用";  
  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 = @"網(wǎng)絡(luò)可用";  
  30.      self.notificationLabel.backgroundColor = [UIColor greenColor];  
  31.       
  32.      if (reach.isReachableViaWiFi) {  
  33.           self.wifiOnlyLabel.backgroundColor = [UIColor greenColor];  
  34.           self.wifiOnlyLabel.text = @"當(dāng)前通過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 = @"當(dāng)前通過2g or 3g連接";  
  43.      } else {  
  44.           self.wwanOnlyLabel.backgroundColor = [UIColor redColor];  
  45.           self.wwanOnlyLabel.text = @"2g or 3g網(wǎng)絡(luò)未使用";  
  46.      }  
  47. }  

 

 

附件demo說明

開啟wifi狀態(tài)

 關(guān)閉wifi的狀態(tài)

 

 

 

遺留問題

  1. 如何在多個(gè)controller之前共用一個(gè)Reachability(附件demo中是一個(gè)controller一個(gè)Reachability實(shí)例)
  2. 應(yīng)該在什么使用停止Reachability的檢測.

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 民乐县| 昌江| 江安县| 密山市| 哈尔滨市| 甘德县| 汝州市| 大理市| 信丰县| 思南县| 玛沁县| 栾城县| 陈巴尔虎旗| 永吉县| 日喀则市| 奈曼旗| 棋牌| 镇安县| 丰顺县| 扬中市| 曲周县| 岳阳县| 固安县| 两当县| 南丹县| 宝兴县| 文化| 宝清县| 昌乐县| 延长县| 富平县| 施秉县| 萨嘎县| 安乡县| 小金县| 新竹市| 武汉市| 独山县| 海口市| 鄢陵县| 德庆县|