IOS代碼筆記之網(wǎng)絡(luò)嗅探功能
本文實(shí)例為大家分享了IOS網(wǎng)絡(luò)嗅探工具,供大家參考,具體內(nèi)容如下
一、效果圖
二、工程圖
三、代碼
AppDelegate.h
#import <UIKit/UIKit.h> #import "Reachability.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> { Reachability *reachability; BOOL WarningViaWWAN; } @property (strong, nonatomic) UIWindow *window; - (void)ReachabilitySniff:(Reachability*) curReach; - (void)ReachabilitySniffNotification:(NSNotification* )notification; - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; @end
AppDelegate.m
#import "AppDelegate.h" #import "RootViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. RootViewController *rootVC=[[RootViewController alloc]init]; UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:rootVC]; self.window.rootViewController=nav; //啟動(dòng)網(wǎng)絡(luò)嗅探功能 WarningViaWWAN = TRUE; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReachabilitySniffNotification:) name:kReachabilityChangedNotification object:nil]; if (!reachability) { reachability = [Reachability reachabilityForInternetConnection]; } [reachability startNotifier]; [self performSelector:@selector(ReachabilitySniff:) withObject:reachability afterDelay:20]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } #pragma mark -網(wǎng)絡(luò)嗅探 - (void)ReachabilitySniffNotification:(NSNotification* )notification { Reachability* curReach = [notification object]; [self performSelector:@selector(ReachabilitySniff:) withObject:curReach afterDelay:2]; } - (void)ReachabilitySniff:(Reachability*) curReach { NSLog(@"ReachabilitySniffNewWorkStatus"); if (!curReach) { return; } NetworkStatus status = [curReach currentReachabilityStatus]; switch (status) { case ReachableViaWiFi: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您正在使用WiFi網(wǎng)絡(luò)" message:Nil delegate:self cancelButtonTitle:Nil otherButtonTitles:@"本次不再提醒",@"知道了", nil]; [alert show]; break; } case ReachableViaWWAN: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您正在使用移動(dòng)網(wǎng)絡(luò),運(yùn)營(yíng)商會(huì)收取流量費(fèi),建議使用WiFi網(wǎng)絡(luò)" message:Nil delegate:self cancelButtonTitle:Nil otherButtonTitles:@"本次不再提醒",@"知道了", nil]; [alert show]; break; } case NotReachable: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"沒(méi)有網(wǎng)絡(luò)" message:Nil delegate:self cancelButtonTitle:Nil otherButtonTitles:@"本次不再提醒",@"知道了", nil]; [alert show]; break; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果
不知道大家發(fā)現(xiàn)沒(méi)有蘋(píng)果在iOS7.0之后,很多系統(tǒng)界面都使用了毛玻璃效果,增加了界面的美觀性,所以這篇文章跟大家分享個(gè)iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果的方法,有需要的可以參考借鑒,下面來(lái)一起看看。2016-09-09iOS中利用CAGradientLayer繪制漸變色的方法實(shí)例
有時(shí)候iOS開(kāi)發(fā)中需要使用到漸變色,來(lái)給圖片或者view蓋上一層,使其顯示效果更好,所以這篇文章主要給大家介紹了關(guān)于iOS中利用CAGradientLayer繪制漸變色的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11iOS開(kāi)發(fā)網(wǎng)絡(luò)篇—實(shí)現(xiàn)大文件的多線程斷點(diǎn)下載
iOS開(kāi)發(fā)中經(jīng)常會(huì)用到文件的下載功能,這篇文章主要介紹了iOS開(kāi)發(fā)網(wǎng)絡(luò)篇—實(shí)現(xiàn)大文件的多線程斷點(diǎn)下載,今天咱們來(lái)分享一下思路。2016-11-11在iOS中使用OpenGL ES實(shí)現(xiàn)繪畫(huà)板的方法
這篇文章主要介紹了在iOS中使用OpenGL ES實(shí)現(xiàn)繪畫(huà)板的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03iOS實(shí)現(xiàn)圖片壓縮的兩種方法及圖片壓縮上傳功能
ios 圖片壓縮有兩種方法,分別是,壓縮圖片質(zhì)量(Quality),壓縮圖片尺寸(Size),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01iOS UIBezierPath實(shí)現(xiàn)餅狀圖
這篇文章主要為大家詳細(xì)介紹了iOS UIBezierPath實(shí)現(xiàn)餅狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03