IOS使用Reachability实时检测网络连接状况
2016-03-09 11:28
471 查看
OS使用Reachability实时检测网络连接状况
//在程序的启动处,开启通知
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//.....
//开启网络状况的监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach =[[Reachability reachabilityWithHostName:@"www.google.com"] retain];//可以以多种形式初始化
[hostReach startNotifier]; //开始监听,会启动一个run
loop
[self updateInterfaceWithReachability: hostReach];
//.....
}
// 连接改变
- (void)reachabilityChanged: (NSNotification*)note
{
Reachability*curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
//处理连接改变后的情况
- (void)updateInterfaceWithReachability: (Reachability*)curReach
{
//对连接改变做出响应的处理动作。
NetworkStatus status=[curReach currentReachabilityStatus];
if (status== NotReachable) { //没有连接到网络就弹出提实况
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"MyApp
Name"
message:@"NotReachable"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
//在程序的启动处,开启通知
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//.....
//开启网络状况的监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach =[[Reachability reachabilityWithHostName:@"www.google.com"] retain];//可以以多种形式初始化
[hostReach startNotifier]; //开始监听,会启动一个run
loop
[self updateInterfaceWithReachability: hostReach];
//.....
}
// 连接改变
- (void)reachabilityChanged: (NSNotification*)note
{
Reachability*curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
//处理连接改变后的情况
- (void)updateInterfaceWithReachability: (Reachability*)curReach
{
//对连接改变做出响应的处理动作。
NetworkStatus status=[curReach currentReachabilityStatus];
if (status== NotReachable) { //没有连接到网络就弹出提实况
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"MyApp
Name"
message:@"NotReachable"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
相关文章推荐
- 使用802.1X+FreeRadius+LDAP实现网络准入方案
- httpclient调用cxf开发webservice接口
- 第三方网络支持库AFNetworking(终端搭建)
- 网络最大流算法之 Ford_Fulkerson方法,EK算法 c++模板
- HDU 3468 Treasure Hunting dijkstra+网络流解决的二分匹配
- 神经网络中的激活函数
- TCP/IP详解学习笔记
- 新开网络处理线程 不返回主线程处理UI刷新的报错
- lzg_ad:XPE网络功能组件
- http 错误代码表
- 基于文本内容的网络行为分析
- Linux就这个范儿 第11章 独霸网络的蜘蛛神功
- unnable to create virtual device:server returned http status code 0
- iOS xcode报错 App Transport Security has blocked a cleartext HTTP (http://) resource load的解决办法
- 网络信息安全技术的学习感悟
- TCP协议要点和难点全解
- HTTP/2 新特性浅析
- 1. Netty解决Tcp粘包拆包
- HTTP权威指南 第七章 缓存
- android和httpClient