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

 更新时间:2016年07月06日 10:21:30   作者:情深雨蒙  
这篇文章主要为大家详细介绍了IOS网络嗅探功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

您可能感兴趣的文章:

相关文章

  • 简单掌握iOS应用开发中sandbox沙盒的使用

    简单掌握iOS应用开发中sandbox沙盒的使用

    这篇文章主要介绍了iOS应用开发中sandbox沙盒的使用,即将应用的存储区域单独隔离开来,开发时经常可以用到,需要的朋友可以参考下
    2016-01-01
  • MAUI模仿iOS多任务切换卡片滑动的交互实现代码

    MAUI模仿iOS多任务切换卡片滑动的交互实现代码

    这篇文章主要介绍了[MAUI]模仿iOS多任务切换卡片滑动的交互实现,使用.NET MAU实现跨平台支持,本项目可运行于Android、iOS平台,需要的朋友可以参考下
    2023-05-05
  • 详解Objective C 中Block如何捕获外部值

    详解Objective C 中Block如何捕获外部值

    这篇文章主要为大家介绍了详解Objective C 中Block如何捕获外部值实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • IOS实现百度地图自定义大头针和气泡样式

    IOS实现百度地图自定义大头针和气泡样式

    这篇文章主要介绍了 IOS百度地图自定义大头针和气泡的实例代码,非常不错,具有参考借鉴价值,需要的朋友参考下
    2016-12-12
  • iOS实现转盘效果

    iOS实现转盘效果

    这篇文章主要为大家详细介绍了iOS实现转盘效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • iOS开发之触摸事件以及手势

    iOS开发之触摸事件以及手势

    这篇文章主要为大家详细介绍了iOS开发之触摸事件以及手势的相关资料,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • iOS 中KVC、KVO、NSNotification、delegate 总结及区别

    iOS 中KVC、KVO、NSNotification、delegate 总结及区别

    这篇文章主要介绍了iOS 中KVC、KVO、NSNotification、delegate 总结及区别的相关资料,需要的朋友可以参考下
    2016-10-10
  • 简单说说iOS之WKWebView的用法小结

    简单说说iOS之WKWebView的用法小结

    iOS8.0之后我们使用 WebKit框架中的WKWebView来加载网页。这篇文章主要介绍了简单说说iOS之WKWebView的用法小结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • iOS中的类、元类以及isa示例详解

    iOS中的类、元类以及isa示例详解

    从初学OC的时候就听人提起过OC对象中的isa指针,用来指向对象所属的类,从而可以在调用方法时通过isa指针找到相应的方法和属性,下面这篇文章主要给大家介绍了关于iOS中类、元类以及isa的相关资料,需要的朋友可以参考借鉴,下面来一起学习学习吧。
    2018-01-01
  • Flutter Widgets MediaQuery控件屏幕信息适配

    Flutter Widgets MediaQuery控件屏幕信息适配

    这篇文章主要为大家介绍了Flutter Widgets 之 MediaQuery控件获取屏幕信息和屏幕适配示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11

最新评论