Reachability网络情况之详解
2015-10-14 20:28
260 查看
Reachability判断网络情况
// 把Reachability声明成属性@property (strong , nonatomic ) Reachability *reach;
//网络监听实现
-(void)setNetNotice
{
//开启网络状况监听 测试服务器状态
self.reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
[self.reach startNotifier];//开始监听.会启动一个run loop kReachabilityChangedNotification
}
//通知
- (void)reachabilityChanged:(NSNotification *)note{
self.reach = [note object];
NSParameterAssert([self.reach isKindOfClass:[Reachability class]]);
NetworkStatus status = [self.reach currentReachabilityStatus];
// 网络没有连接时
if(status == NotReachable){
// 弹出提示框
UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:nil message:@"网络已断开" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alterView show];
}else if(status == ReachableViaWWAN){ // 使用流量时
UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:nil message:@"当前为2G/3G网络,请注意流量" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alterView show];
}else if(status == ReachableViaWiFi){ // 链接wifi时
UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:nil message:@"当前为WiFi网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alterView show];
}
}
相关文章推荐
- ArcGIS网络分析之构建网络分析数据集与网络发布服务
- java Webservice(一)HttpClient远程调用
- 深度学习概述:从感知机到深度网络
- BP误差反传神经网络
- angular $http配置属性
- transaction 用tx事务 测试时 报错:Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc]
- 面试题——TCP和UDP区别
- 从网络下载apk并自动安装
- 使用Service实现网络音乐下载并播放
- iOS解决http请求不能使用问题
- 网络加载图片缓存与SDWebImage
- Android5.1网络切换策略分析
- linux c 实现 http post的代码-1
- 计算机网络基础知识总结
- iOS开发网络数据之AFNetworking使用
- UDP、TCP
- iOS 网络请求的第三方比对
- iis7 发布mvc3 遇到的HTTP错误 403.14-Forbidden
- 网络通信三要素:IP、端口、协议
- HTTP 学习