IOS代码笔记之网络嗅探功能

时间:2022-04-03 14:22:22

本文实例为大家分享了ios网络嗅探工具,供大家参考,具体内容如下

一、效果图 

IOS代码笔记之网络嗅探功能

二、工程图

 IOS代码笔记之网络嗅探功能

三、代码
appdelegate.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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;
    }
  }
}

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。