The network sniffing function of IOS code notes _ios

Source: Internet
Author: User
Tags uikit

This example for you to share the iOS network sniffer tools for your reference, the specific contents are as follows

One, the effect chart

Second, engineering drawing

Third, the 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 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;
  
  Start Network sniffing function 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-Network sniffer-(void) Reachabilitysniffnotification: (nsnotification*) Notification {reachability* Curreach = [No
  Tification 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:@ "You are using WI
      Fi network "Message:nil delegate:self cancelbuttontitle:nil otherbuttontitles:@" This time no longer remind, @ "Know", Nil];
      [Alert show];
    Break Case Reachableviawwan: {Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "You are using mobile network, operators will charge a traffic fee, suggest
  Use WiFi network "Message:nil delegate:self cancelbuttontitle:nil otherbuttontitles:@" This time no longer remind, @ "Know", 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", @ "Know", Nil];
      [Alert show];
    Break
 }
  }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.