服务器之家

服务器之家 > 正文

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

时间:2021-01-21 15:35     来源/作者:情深雨蒙

本文实例为大家分享了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;
    }
  }
}

 

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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部