Reachability的使用
刚到一家新公司 做新项目 关于网络状态的监听和同事产生了不一样的看法 原来我的网络监听都是自己写的 后来发现自己不是一般的傻 有一个叫做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的使用的更多相关文章
- iOS网络4——Reachability检测网络状态
一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发. 其实在网络开发中还有比较常用的就是网络 ...
- 通读AFN③--HTTPS访问控制(AFSecurityPolicy),Reachability(AFNetworkReachabilityManager)
这一篇主要介绍使用AFN如何访问HTTPS网站以及这些做法的实现原理,还有介绍AFN的网络状态监测部分AFNetworkReachabilityManager,这个模块会和苹果官方推荐的Reachab ...
- 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 ...
- IOS开发之网络编程开源类 Reachability应用
先看Reachability.h发现 #import <Foundation/Foundation.h> #import <SystemConfiguration/SystemCon ...
- iOS中使用 Reachability 检测网络
iOS中使用 Reachability 检测网络 内容提示:下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Reachability都 ...
- Reachability(判断网络是否连接)
类似于一个网络状况的探针. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabili ...
- iOS开发 利用Reachability判断网络环境
导入头文件:#import "Reachability.h" 然后将 SystemConfiguration.framework 添加进工程: 1.检查当前的网络状态(wifi.W ...
- IOS 网络浅析(一 网络监测~Reachability)
网络监测应用于各种需要连接网络的app设计,由于现在开发的app几乎都用到网络,因此,网络监测也成为了较为重点的知识,下面我给大家简单讲解一下网络监测的实际应用,依旧会有代码哦. 想要实现网络监测,可 ...
- iOS网络检测Reachability 使用 Demo,可检测2、3、4G
你可以在Github下载这个Demo https://github.com/JanzTam/Reachability_Demo 首先,引入系统的Reachability类,不知道怎么引入的话,在Xco ...
- iOS开发网络篇—Reachability检测网络状态
前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...
随机推荐
- 『题解』洛谷P1351 联合权值
更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...
- Anaconda的安装和详细介绍(带图文)
Anacond的介绍 Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项. 因为包含了大量的科学包,Anaconda 的下载文件比较大( ...
- windows备份mysql数据库
1.编写执行文件mysql_backup.bat rem auther:ELSON ZENGrem date:20191104rem mysql backup! @echo off set mysql ...
- Java基础语法01
一.Java入门 Java 是最好的语言吗? 不是,因为在每个领域都有更合适的编程语言. Java技术体系平台 JavaSE//JavaEE//JavaME Java程序的结构 类{ 方法{ 语句; ...
- php PDO getlastsql写法
php PDO getlastsql写法有些时候 运行失败需要查看 sql语句 原型有没有语法错误 这个时候就用 下面的函数就是把问号替换成 值 就可以看到原型了<pre>function ...
- 在Debian/Ubuntu上面安装升级nginx到最新版
在Debian下面通过 apt-get 可以自动安装 nginx,不过版本一般比较老,如果想要使用nginx的最新特性就需要升级版本. 一般安装可以通过编绎源文件安装,但可能需要安装很多编绎工具, ...
- mysql提示Packet for query is too large (1142 > 1024)解决方案
注:最近mysql一直提示如下错误 Packet for query is too large (1185 > 1024). You can change this value on the s ...
- 前端与算法 leetcode 7. 整数反转
目录 # 前端与算法 leetcode 7. 整数反转 题目描述 概要 提示 解析 解法 算法 传入测试用例的运行结果 执行结果 GitHub仓库 # 前端与算法 leetcode 7. 整数反转 题 ...
- C语言1博客作业06
这个作业属于哪个课程 C语言程序设计II 这个作业的要求在哪里 https://www.cnblogs.com/sanying/p/11771502.html 我在这个课程的目标是 端正态度,认真对待 ...
- shell——数组
默认从0开始索引:也可以单独(像字典一样)pid[35420]=httpd -k ssl, 只能是一维的 bash4.0增加了关联数组 数组赋值: declare -a myarray声明数组 一次一 ...