[iOS 多線程 & 網路 - 2.8] - 檢測網路狀態

來源:互聯網
上載者:User

標籤:

A、說明在網路應用中,需要對使用者裝置的網路狀態進行即時監控,有兩個目的:
(1)讓使用者瞭解自己的網路狀態,防止一些誤會(比如怪應用無能)
(2)根據使用者的網路狀態進行智能處理,節省使用者流量,提高使用者體驗
  WIFI\3G網路:自動下載高清圖片
  低速網路:只下載縮圖  沒有網路:只顯示離線的快取資料 蘋果官方提供了一個叫Reachability的樣本程式,便於開發人員檢測網路狀態
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 注意:此功能只能用真機測試,iOS模擬器,一直都有wifi訊號,即使關掉或者使用飛航模式. B、監測網路狀態Reachability的使用步驟添加架構SystemConfiguration.framework包含標頭檔#import "Reachability.h"
 1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 2     // 檢測wifi狀態 3     Reachability *wifiStatus = [Reachability reachabilityForLocalWiFi]; 4     5     // 檢測行動電話通訊狀態 6     Reachability *netStatus = [Reachability reachabilityForInternetConnection]; 7     8     // 判斷網路狀態 9     if ([wifiStatus currentReachabilityStatus] != NotReachable) {10         NSLog(@"正在使用wifi上網");11     } else if ([netStatus currentReachabilityStatus] != NotReachable) {12         NSLog(@"正在使用行動電話通訊上網");13     } else {14         NSLog(@"沒有網路");15     }  16 }17  
 C.即時監測網路狀態使用通知監控網路狀態類要發送通知給控制器銷毀控制器的時候一定要刪除通知、停止發送訊息 
 1 // 2 //  ViewController.m 3 //  ReachabilityDemo 4 // 5 //  Created by hellovoidworld on 15/1/28. 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8  9 #import "ViewController.h"10 #import "Reachability.h"11 12 @interface ViewController ()13 14 @property(nonatomic, strong) Reachability *networkStatus;15 16 @end17 18 @implementation ViewController19 20 - (void)viewDidLoad {21     [super viewDidLoad];22     // Do any additional setup after loading the view, typically from a nib.23    24     // 即時監控手機連網狀態25     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectNetworkStatus) name:kReachabilityChangedNotification object:nil];26    27     // 開啟通知發送28     self.networkStatus = [Reachability reachabilityForInternetConnection];29     [self.networkStatus startNotifier];30 }31 32 - (void)dealloc {33     // 停止發送通知34     [self.networkStatus stopNotifier];35    36     // 切記要刪除通知37     [[NSNotificationCenter defaultCenter] removeObserver:self];38 }39 40 // 用WIFI41 // [wifi currentReachabilityStatus] != NotReachable42 // [conn currentReachabilityStatus] != NotReachable43 44 // 沒有用WIFI, 只用了行動電話通訊45 // [wifi currentReachabilityStatus] == NotReachable46 // [conn currentReachabilityStatus] != NotReachable47 48 // 沒有網路49 // [wifi currentReachabilityStatus] == NotReachable50 // [conn currentReachabilityStatus] == NotReachable51 52 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {53     [self detectNetworkStatus];54 }55 56 - (void) detectNetworkStatus {57     // 檢測wifi狀態58     Reachability *wifiStatus = [Reachability reachabilityForLocalWiFi];59    60     // 檢測行動電話通訊狀態61     Reachability *netStatus = [Reachability reachabilityForInternetConnection];62    63     // 判斷網路狀態64     if ([wifiStatus currentReachabilityStatus] != NotReachable) {65         NSLog(@"正在使用wifi上網");66     } else if ([netStatus currentReachabilityStatus] != NotReachable) {67         NSLog(@"正在使用行動電話通訊上網");68     } else {69         NSLog(@"沒有網路");70     }71 }72 73 @end74  

 

 

[iOS 多線程 & 網路 - 2.8] - 檢測網路狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.