一般有兩種方式,都是第三方的框架,輪子嘛,能用就先用著,後面再優化。
一:Reachability
1.首先在AppDelegate.h新增標頭檔案"Reachability.h",匯入框架SystemConfiguration.frame。
2. 在AppDelegate.m中這樣實現:
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 2 //開啟網路狀況的監聽 3 //來訂閱實時的網路狀態變化通知。匯入Reachability.h標頭檔案,然後註冊一個物件來訂閱網路狀態變化的資訊,網路狀態變化的資訊名稱為kReachabilityChanged-Notification 4 5 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; 6 //通過檢查某個主機能否訪問來判斷當前網路是否可用: 7 self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ; 8 9 //開始監聽,會啟動一個run loop 10 [self.hostReach startNotifier]; 11 12 } 13 14 -(void)reachabilityChanged:(NSNotification *)note{ 15 Reachability *currReach = [note object]; 16 17 NSParameterAssert([currReach isKindOfClass:[Reachability class]]); 18 19 //對連線改變做出響應處理動作 20 21 NetworkStatus status = [currReach currentReachabilityStatus]; 22 23 //如果沒有連線到網路就彈出提醒實況 24 25 self.isReachable = YES; 26 27 if(status == NotReachable){ 28 29 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網路連線異常" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; 30 [alert show]; 31 [alert release]; 32 self.isReachable = NO; 33 return; 34 35 } 36 37 if (status==kReachableViaWiFi||status==kReachableViaWWAN) { 38 39 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網路連線資訊" message:@"網路連線正常" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; 40 41 // [alert show]; 42 43 [alert release]; 44 self.isReachable = YES; 45 46 } 47 } 48 49 然後在每個頁面的viewWillAppear:加上: 50 51 -(void)viewWillAppear:(BOOL)animated{ 52 [super viewWillAppear:YES]; 53 AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 54 if(appDlg.isReachable){ 55 NSLog(@"網路已連線");//執行網路正常時的程式碼 56 } 57 else{ 58 NSLog(@"網路連線異常");//執行網路異常時的程式碼 59 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網路連線異常" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; 60 [alert show]; 61 [alert release]; 62 } 63 }
這樣就可以檢查到在執行程式時網路突然的中斷和連線。Reachability類實際上是蘋果公司對SCNetworkReachability API的封裝,這個API定義在SystemConfigure.framework庫中。如果有其他特別的需求,也可以直接使用這個原生的SCNetworkReachability類。
二:AFNetworking監測
1.匯入框架,和標頭檔案#import <AFNetworkReachabilityManager.h>
2.程式碼:
1 -(void)afn{ 2 //1.建立網路狀態監測管理者 3 AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager]; 4 //開啟監聽,記得開啟,不然不走block 5 [manger startMonitoring]; 6 //2.監聽改變 7 [manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 8 /* 9 AFNetworkReachabilityStatusUnknown = -1, 10 AFNetworkReachabilityStatusNotReachable = 0, 11 AFNetworkReachabilityStatusReachableViaWWAN = 1, 12 AFNetworkReachabilityStatusReachableViaWiFi = 2, 13 */ 14 switch (status) { 15 case AFNetworkReachabilityStatusUnknown: 16 NSLog(@"未知"); 17 break; 18 case AFNetworkReachabilityStatusNotReachable: 19 NSLog(@"沒有網路"); 20 break; 21 case AFNetworkReachabilityStatusReachableViaWWAN: 22 NSLog(@"3G|4G"); 23 break; 24 case AFNetworkReachabilityStatusReachableViaWiFi: 25 NSLog(@"WiFi"); 26 break; 27 default: 28 break; 29 } 30 }]; 31 }