IOS代码笔记之网络嗅探功能
程序员文章站
2023-12-10 13:39:16
本文实例为大家分享了ios网络嗅探工具,供大家参考,具体内容如下
一、效果图
二、工程图
三、代码
appdeleg...
本文实例为大家分享了ios网络嗅探工具,供大家参考,具体内容如下
一、效果图
二、工程图
三、代码
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; //启动网络嗅探功能 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 -网络嗅探 - (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网络" message:nil delegate:self cancelbuttontitle:nil otherbuttontitles:@"本次不再提醒",@"知道了", nil]; [alert show]; break; } case reachableviawwan: { uialertview *alert = [[uialertview alloc] initwithtitle:@"您正在使用移动网络,运营商会收取流量费,建议使用wifi网络" message:nil delegate:self cancelbuttontitle:nil otherbuttontitles:@"本次不再提醒",@"知道了", nil]; [alert show]; break; } case notreachable: { uialertview *alert = [[uialertview alloc] initwithtitle:@"没有网络" message:nil delegate:self cancelbuttontitle:nil otherbuttontitles:@"本次不再提醒",@"知道了", nil]; [alert show]; break; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。