iOS中使用 Reachability 檢測網路,iosreachability

來源:互聯網
上載者:User

iOS中使用 Reachability 檢測網路,iosreachability
iOS中使用 Reachability 檢測網路內容提示:下提供離線模式(Evernote)。那麼你會使用到Reachability來實現網路檢測。   寫本文的目的 瞭解Reachability都能做什麼 檢測3中網路環境 2G/3G wifi 無網路 如何使用通知 單個controller 多個controller 簡單的功能: 僅在wifi下使用 Reachability簡介 Reachablity 是一個iOS下...

 

如果你想在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

 

安裝

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

 

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方式使用

 

1234567891011121314151617181920212223242526272829303132 - (void)viewDidLoad{    [super viewDidLoad];     DLog(@"開啟 www.apple.com 的網路檢測");     Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];     DLog(@"-- current status: %@", reach.currentReachabilityString);          // start the notifier which will cause the reachability object to retain itself!          [[NSNotificationCenter defaultCenter] addObserver:self                                                        selector:@selector(reachabilityChanged:)                                                             name:kReachabilityChangedNotification                                                          object:nil];          reach.reachableBlock = ^(Reachability * reachability)    {        dispatch_async(dispatch_get_main_queue(), ^{            self.blockLabel.text = @"網路可用";               self.blockLabel.backgroundColor = [UIColor greenColor];        });    };        reach.unreachableBlock = ^(Reachability * reachability)    {        dispatch_async(dispatch_get_main_queue(), ^{            self.blockLabel.text = @"網路不可用";               self.blockLabel.backgroundColor = [UIColor redColor];        });    };          [reach startNotifier];}

 

 

使用notification的方式

 

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 - (void)viewDidLoad{    [super viewDidLoad];     DLog(@"開啟 www.apple.com 的網路檢測");     Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];     DLog(@"-- current status: %@", reach.currentReachabilityString);          // start the notifier which will cause the reachability object to retain itself!          [[NSNotificationCenter defaultCenter] addObserver:self                                                        selector:@selector(reachabilityChanged:)                                                             name:kReachabilityChangedNotification                                                          object:nil];     [reach startNotifier];} - (void) reachabilityChanged: (NSNotification*)note {     Reachability * reach = [note object];        if(![reach isReachable])    {        self.notificationLabel.text = @"網路不可用";          self.notificationLabel.backgroundColor = [UIColor redColor];          self.wifiOnlyLabel.backgroundColor = [UIColor redColor];          self.wwanOnlyLabel.backgroundColor = [UIColor redColor];          return;    }             self.notificationLabel.text = @"網路可用";     self.notificationLabel.backgroundColor = [UIColor greenColor];          if (reach.isReachableViaWiFi) {          self.wifiOnlyLabel.backgroundColor = [UIColor greenColor];          self.wifiOnlyLabel.text = @"當前通過wifi串連";     } else {          self.wifiOnlyLabel.backgroundColor = [UIColor redColor];          self.wifiOnlyLabel.text = @"wifi未開啟,不能用";     }          if (reach.isReachableViaWWAN) {          self.wwanOnlyLabel.backgroundColor = [UIColor greenColor];          self.wwanOnlyLabel.text = @"當前通過2g or 3g串連";     } else {          self.wwanOnlyLabel.backgroundColor = [UIColor redColor];          self.wwanOnlyLabel.text = @"2g or 3g網路未使用";     }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.