普通有兩種方法,都是第三方的框架,輪子嘛,能用就先用著,前面再優化。
一:Reachability
1.起首在AppDelegate.h添加頭文件"Reachability.h",導入框架SystemConfiguration.frame。
2. 在AppDelegate.m中如許完成:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ //開啟收集狀態的監聽 //來定閱及時的收集狀況變更告訴。導入Reachability.h頭文件,然後注冊一個對象來定閱收集狀況變更的信息,收集狀況變更的信息稱號為kReachabilityChanged-Notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; //經由過程檢討某個主性能否拜訪來斷定以後收集能否可用: self.hostReach = [Reachability reachabilityWithHostName:@"www.百度.com"] ; //開端監聽,會啟動一個run loop [self.hostReach startNotifier]; } -(void)reachabilityChanged:(NSNotification *)note{ Reachability *currReach = [note object]; NSParameterAssert([currReach isKindOfClass:[Reachability class]]); //對銜接轉變做出呼應處置舉措 NetworkStatus status = [currReach currentReachabilityStatus]; //假如沒有銜接到收集就彈出提示實況 self.isReachable = YES; if(status == NotReachable){ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收集銜接異常" message:nil delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil]; [alert show]; [alert release]; self.isReachable = NO; return; } if (status==kReachableViaWiFi||status==kReachableViaWWAN) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收集銜接信息" message:@"收集銜接正常" delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil]; // [alert show]; [alert release]; self.isReachable = YES; } }
然後在每一個頁面的viewWillAppear:加上:
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:YES]; AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate]; if(appDlg.isReachable){ NSLog(@"收集已銜接");//履行收集正常時的代碼 } else{ NSLog(@"收集銜接異常");//履行收集異常時的代碼 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收集銜接異常" message:nil delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil]; [alert show]; [alert release]; } }
如許便可以檢討到在運轉法式時收集忽然的中止和銜接。Reachability類現實上是蘋果公司對S.networkReachability API的封裝,這個API界說在SystemConfigure.framework庫中。假如有其他特殊的需求,也能夠直接應用這個原生的S.networkReachability類。
二:A.networking監測
1.導入框架,和頭文件#import <AFNetworkReachabilityManager.h>
2.代碼:
-(void)afn{ //1.創立收集狀況監測治理者 AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager]; //開啟監聽,記得開啟,否則不走block [manger startMonitoring]; //2.監聽轉變 [manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { /* AFNetworkReachabilityStatusUnknown = -1, AFNetworkReachabilityStatusNotReachable = 0, AFNetworkReachabilityStatusReachableViaWWAN = 1, AFNetworkReachabilityStatusReachableViaWiFi = 2, */ switch (status) { case AFNetworkReachabilityStatusUnknown: NSLog(@"未知"); break; case AFNetworkReachabilityStatusNotReachable: NSLog(@"沒有收集"); break; case AFNetworkReachabilityStatusReachableViaWWAN: NSLog(@"3G|4G"); break; case AFNetworkReachabilityStatusReachableViaWiFi: NSLog(@"WiFi"); break; default: break; } }]; }
以上所述是小編給年夜家引見的IOS 檢測收集狀況的兩種辦法,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對本站網站的支撐!
【iOS 檢測收集狀況的兩種辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!