?
1 // 2 // BWNetWorkTool.h 3 // IOS_0131_檢測網絡狀態 4 // 5 // Created by ma c on 16/1/31. 6 // Copyright ? 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface BWNetWorkTool : NSObject 12 ///是否是WiFi 13 + (BOOL)isEnableWiFi; 14 ///是否是3G 15 + (BOOL)isEnable3G; 16 17 @end 18 19 // 20 // BWNetWorkTool.m 21 // IOS_0131_檢測網絡狀態 22 // 23 // Created by ma c on 16/1/31. 24 // Copyright ? 2016年 博文科技. All rights reserved. 25 // 26 27 #import "BWNetWorkTool.h" 28 #import "Reachability.h" 29 30 @implementation BWNetWorkTool 31 32 //是否是WiFi 33 + (BOOL)isEnableWiFi 34 { 35 return [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] !=NotReachable; 36 } 37 //是否是3G 38 + (BOOL)isEnable3G 39 { 40 return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] !=NotReachable; 41 } 42 43 44 @end
1 // 2 // ViewController.m 3 // IOS_0131_檢測網絡狀態 4 // 5 // Created by ma c on 16/1/31. 6 // Copyright ? 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "Reachability.h" 11 #import "BWNetWorkTool.h" 12 13 @interface ViewController () 14 15 @property (nonatomic, strong) Reachability *reachability; 16 17 @end 18 19 @implementation ViewController 20 /* 21 檢測網絡狀態 22 1.在網絡應用中,需要對用戶設備的網絡狀態進行實時監控,目的: 23 a.讓用戶了解自己的網絡狀態,防止一些誤會(怪應用無能) 24 b.根據用戶的網絡狀態進行智能處理,節省用戶流量,提高用戶體驗 25 WIFI/3G/4G網絡:自動下載高清圖片 26 低速網絡:只能下載縮略圖 27 沒有網絡:智能顯示離線的緩存數據 28 c.蘋果官方提供了一個叫Reachability的示例程序,便于開發者檢測網絡狀態 29 https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 30 31 2.Reachability的使用步驟 32 1>添加框架SystemConfiguration.framework 33 2>添加源代碼 34 3>包含頭文件 - #import "Reachability.h" 35 36 3.常見用法 37 1>是否是WiFi 38 + (BOOL)isEnableWiFi 39 { 40 return [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] !=NotReachable; 41 } 42 2>是否是3G 43 + (BOOL)isEnable3G 44 { 45 return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] !=NotReachable; 46 } 47 */ 48 49 - (void)viewDidLoad { 50 [super viewDidLoad]; 51 52 //監聽網絡狀態發生改變通知 53 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil]; 54 55 //獲得Reachability對象 56 self.reachability = [Reachability reachabilityForInternetConnection]; 57 58 //開始監控 59 [self.reachability startNotifier]; 60 61 // //獲取Reachability對象 62 // Reachability *wifi = [Reachability reachabilityForLocalWiFi]; 63 // //獲取Reachability對象的當前網絡狀態 64 // NetworkStatus wifiStatus = wifi.currentReachabilityStatus; 65 // 66 // if (wifiStatus !=NotReachable) { 67 // NSLog(@"wifi"); 68 // } 69 70 } 71 72 - (void)networkStateChange 73 { 74 NSLog(@"網絡狀態改變了"); 75 [self changeNetworkState]; 76 } 77 78 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 79 { 80 [self changeNetworkState]; 81 } 82 83 - (void)changeNetworkState 84 { 85 if ([BWNetWorkTool isEnableWiFi]) { 86 NSLog(@"WiFi環境"); 87 }else if ([BWNetWorkTool isEnable3G]){ 88 NSLog(@"手機自帶網絡"); 89 }else{ 90 NSLog(@"沒有網絡"); 91 } 92 } 93 94 - (void)dealloc 95 { 96 [self.reachability stopNotifier]; 97 [[NSNotificationCenter defaultCenter] removeObserver:self]; 98 } 99 100 @end
?