[Code Note] network sniffing and code note sniffing
I ,.
2. Engineering Drawing.
3. Code.
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 didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]; // Override point for customization after application launch. rootViewController * rootVC = [[RootViewController alloc] init]; UINavigat IonController * nav = [[UINavigationController alloc] initWithRootViewController: rootVC]; self. window. rootViewController = nav; // enable the network sniffing function WarningViaWWAN = TRUE; [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (handler :) name: kReachabilityChangedNotification object: nil]; if (! Reachability) {reachability = [Reachability failed];} [reachability startNotifier]; [self defined mselector: @ selector (ReachabilitySniff :) withObject: reachability afterDelay: 20]; self. window. backgroundColor = [UIColor whiteColor]; [self. window makeKeyAndVisible]; return YES ;}# pragma mark-network sniffing-(void) ReachabilitySniffNotification :( NSNotification *) notification {Reachabilit Y * curReach = [notification object]; [self defined mselector: @ 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: @ "you are using a Wi-Fi network" message: Nil delegate: self cancelButtonTitle: Nil otherButtonTitles: @ "no longer remind me this time", @ "OK", nil]; [alert show]; break;} case ReachableViaWWAN: {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "you are using a mobile network, and the carrier will charge the traffic fee. We recommend using a Wi-Fi network" message: nil delegate: self cancelButtonTitle: Nil otherButtonTitles: @ "this time no longer reminds me", @ "", nil]; [alert show]; break;} case NotReachable: {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "No network" message: Nil delegate: self cancelButtonTitle: Nil otherButtonTitles: @ "this time no longer reminds me ", @ "OK", nil]; [alert show]; break ;}}}