刚到一家新公司 做新项目 关于网络状态的监听和同事产生了不一样的看法 原来我的网络监听都是自己写的 后来发现自己不是一般的傻 有一个叫做Reachability的东西 很简单 很实用 很暴力 下面就是使用方法

首先在AppDelegate.h添加头文件"Reachability.h",导入框架SystemConfiguration.frame

下面是代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

//开启网络状况的监听

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;

//开始监听,会启动一个run loop

[self.hostReach startNotifier];

}

-(void)reachabilityChanged:(NSNotification *)note

{

Reachability *currReach = [note object];

NSParameterAssert([currReach isKindOfClass:[Reachability class]]);

//对连接改变做出响应处理动作

NetworkStatus status = [currReach currentReachabilityStatus];

//如果没有连接到网络就弹出提醒实况

self.isReachable = YES;

if(status == NotReachable)

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

[alert show];

[alert release];

self.isReachable = NO;

return;

}

if (status==kReachableViaWiFi||status==kReachableViaWWAN) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

//        [alert show];

[alert release];

self.isReachable = YES;

}

}

然后在每个页面的viewWillAppear:加上:

-(void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:YES];

AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];

if(appDlg.isReachable)

{

NSLog(@"网络已连接");//执行网络正常时的代码

}

else

{

NSLog(@"网络连接异常");//执行网络异常时的代码

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

[alert show];

[alert release];

}

}

这样就可以检查到在运行程序时网络突然的中断和连接。

ok了 是不是so NB

Reachability的使用的更多相关文章

  1. iOS网络4——Reachability检测网络状态

    一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发. 其实在网络开发中还有比较常用的就是网络 ...

  2. 通读AFN③--HTTPS访问控制(AFSecurityPolicy),Reachability(AFNetworkReachabilityManager)

    这一篇主要介绍使用AFN如何访问HTTPS网站以及这些做法的实现原理,还有介绍AFN的网络状态监测部分AFNetworkReachabilityManager,这个模块会和苹果官方推荐的Reachab ...

  3. 3-HOP: A High-Compression Indexing Scheme for Reachability Query

    title: 3-HOP: A High-Compression Indexing Scheme for Reachability Query venue: SIGMOD'09 author: Ruo ...

  4. IOS开发之网络编程开源类 Reachability应用

    先看Reachability.h发现 #import <Foundation/Foundation.h> #import <SystemConfiguration/SystemCon ...

  5. iOS中使用 Reachability 检测网络

    iOS中使用 Reachability 检测网络 内容提示:下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测.   写本文的目的 了解Reachability都 ...

  6. Reachability(判断网络是否连接)

    类似于一个网络状况的探针. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabili ...

  7. iOS开发 利用Reachability判断网络环境

    导入头文件:#import "Reachability.h" 然后将 SystemConfiguration.framework 添加进工程: 1.检查当前的网络状态(wifi.W ...

  8. IOS 网络浅析(一 网络监测~Reachability)

    网络监测应用于各种需要连接网络的app设计,由于现在开发的app几乎都用到网络,因此,网络监测也成为了较为重点的知识,下面我给大家简单讲解一下网络监测的实际应用,依旧会有代码哦. 想要实现网络监测,可 ...

  9. iOS网络检测Reachability 使用 Demo,可检测2、3、4G

    你可以在Github下载这个Demo https://github.com/JanzTam/Reachability_Demo 首先,引入系统的Reachability类,不知道怎么引入的话,在Xco ...

  10. iOS开发网络篇—Reachability检测网络状态

    前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...

随机推荐

  1. 腾讯正式开源图计算框架Plato,十亿级节点图计算进入分钟级时代

    腾讯开源再次迎来重磅项目,14日,腾讯正式宣布开源高性能图计算框架Plato,这是在短短一周之内,开源的第五个重大项目. 相对于目前全球范围内其它的图计算框架,Plato可满足十亿级节点的超大规模图计 ...

  2. python经典算法题:Z字变形

    题目 直接看图! 思路第一步:分组 我们把传入的字符串进行分组, 每个框内的字母为1组: 我们发现每个相同颜色的框内的一组字母的特点是在传入的字符串中是连续的: 我们还发现每组字母的个数是由numRo ...

  3. python手册学习笔记1

    笔记1 > http://www.pythondoc.com/pythontutorial3/controlflow.html 参数传递 Python中sys.argv的用法 调用解释器时,脚本 ...

  4. Mysql中,update语句引起的时间戳自动更新问题

    前几天遇到一个奇怪的问题. 在Mysql数据库中有一张表,表中有一个字段是timestamp类型的.我在update别的字段时,这个timestamp字段的时间会自动更新为当前时间. 后来发现,是My ...

  5. Look into Bitmap images

    What's a Bitmap image? I'm not going to explain the differences between raster and vector images, no ...

  6. [LC]530题 二叉搜索树的最小绝对差

    ①题目 给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值. 示例 : 输入: 1   \   3  / 2 输出:1 解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

  7. Spring简单的示例

    参考资料:https://how2j.cn/k/spring/spring-ioc-di/87.html.https://www.w3cschool.cn/wkspring/dgte1ica.html ...

  8. saprk性能调优参考

    1.Tuning Spark 文档 原文:http://spark.apache.org/docs/latest/tuning.html 翻译参考:https://www.cnblogs.com/lh ...

  9. C语言|博客作业04

    这个作业属于哪个课程 C语言程序设计II 这个作业的要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-1/homework/9768 我在这个课程的 ...

  10. Springboot 系列(十六)你真的了解 Swagger 文档吗?

    前言 目前来说,在 Java 领域使用 Springboot 构建微服务是比较流行的,在构建微服务时,我们大多数会选择暴漏一个 REST API 以供调用.又或者公司采用前后端分离的开发模式,让前端和 ...