iOS開發 - 檢測網路狀態(WIFI、2G/3G/4G)
檢測網路狀態
在網路應用中,需要對使用者裝置的網路狀態進行即時監控,目的是
讓使用者瞭解自己的網路狀態,防止一些誤會(比如怪應用無能)
根據使用者的網路狀態進行智能處理,節省使用者流量,提高使用者體驗
WIFI\3G網路:自動下載高清圖片
低速網路:只下載縮圖
沒有網路:只顯示離線的快取資料
蘋果官方提供了一個叫Reachability的樣本程式,便於開發人員檢測網路狀態
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
Reachability的使用步驟
添加架構SystemConfiguration.framework
添加原始碼
包含標頭檔
#import "Reachability.h"
抽取工具類
常見用法// 是否WIFI+ (BOOL) IsEnableWIFI { return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 是否3G+ (BOOL) IsEnable3G { return ([[Reachability reachabilityForInternetConnectionen] currentReachabilityStatus] != NotReachable);}
網路監控
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];self.netReachability = [Reachability reachabilityForInternetConnection];[self.netReachability startNotifier];- (void)dealloc{ [self.netReachability stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];}
檢測網路狀態執行個體
NetWorkTool工具類
#import @interface NetworkTool : NSObject/** * 是否WIFI */+ (BOOL)isEnableWIFI;/** * 是否3G */+ (BOOL)isEnable3G;@end
#import "NetWorkTool.h"#import "Reachability.h"@implementation NetworkTool// 是否WIFI+ (BOOL)isEnableWIFI { return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 是否3G+ (BOOL)isEnable3G { return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);}@end
實作類別
#import "ViewController.h"#import "Reachability.h"#import "NetworkTool.h"@interface ViewController ()@property (nonatomic, strong) Reachability *reachability;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; // 監聽網路狀態發生改變的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil]; // 獲得Reachability對象 self.reachability = [Reachability reachabilityForInternetConnection]; // 開始監控網路 [self.reachability startNotifier];}- (void)dealloc{ [self.reachability stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self];}- (void)networkStateChange{ NSLog(@"網路狀態改變了"); [self checkNetworkState];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self checkNetworkState];}
#pragma mark - App應用程式網路類型改變就會調用- (void)networkStateChange{ if ([NetWorkTool isEnableWIFI]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示您" message:@"您現在網路環境為wifi!開始暢享吧!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; [alert show]; } else if ([NetWorkTool isEnable3G]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示您" message:@"您現在網路環境為3G/4G網路!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; [alert show]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示您" message:@"您當前沒有可連的網路或已經掉線!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; [alert show]; }}