iOS中使用 Reachability 检测网络
来源:互联网 发布:足球战术软件 编辑:程序博客网 时间:2024/05/21 20:30
如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote)。那么你会使用到Reachability来实现网络检测。
写本文的目的
- 了解Reachability都能做什么
- 检测3中网络环境
- 2G/3G
- wifi
- 无网络
- 如何使用通知
- 单个controller
- 多个controller
- 简单的功能:
- 仅在wifi下使用
Reachability简介
Reachablity 是一个iOS下检测,iOS设备网络环境用的库。
- 监视目标网络是否可用
- 监视当前网络的连接方式
- 监测连接方式的变更
苹果官方提供的Doc
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
Github上的文档
https://github.com/tonymillion/Reachability
安装
- 创建 network 工程(network是我创建的demo工程,附件中可以下载到)
- 使用Cocoaspod安装依赖
- 在项目中添加 SystemConfiguration.framework 库
由于Reachability非常常用。直接将其加入到Supporting Files/networ-Prefix.pch中:
C代码
- #import <Reachability/Reachability.h>
如果你还不知道cocoaspod是什么,看这里:
http://witcheryne.iteye.com/blog/1873221
使用
stackoverflow上有一篇回答,很好的解释了reachability的用法
http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability
- 一般情况一个Reachability实例就ok了。
- 一个Controller只需要一个Reachability
Block方式使用
C代码
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- DLog(@"开启 www.apple.com 的网络检测");
- Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
- DLog(@"-- current status: %@", reach.currentReachabilityString);
- // start the notifier which will cause the reachability object to retain itself!
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(reachabilityChanged:)
- name:kReachabilityChangedNotification
- object:nil];
- reach.reachableBlock = ^(Reachability * reachability)
- {
- dispatch_async(dispatch_get_main_queue(), ^{
- self.blockLabel.text = @"网络可用";
- self.blockLabel.backgroundColor = [UIColor greenColor];
- });
- };
- reach.unreachableBlock = ^(Reachability * reachability)
- {
- dispatch_async(dispatch_get_main_queue(), ^{
- self.blockLabel.text = @"网络不可用";
- self.blockLabel.backgroundColor = [UIColor redColor];
- });
- };
- [reach startNotifier];
- }
使用notification的方式
C代码
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- DLog(@"开启 www.apple.com 的网络检测");
- Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
- DLog(@"-- current status: %@", reach.currentReachabilityString);
- // start the notifier which will cause the reachability object to retain itself!
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(reachabilityChanged:)
- name:kReachabilityChangedNotification
- object:nil];
- [reach startNotifier];
- }
- - (void) reachabilityChanged: (NSNotification*)note {
- Reachability * reach = [note object];
- if(![reach isReachable])
- {
- self.notificationLabel.text = @"网络不可用";
- self.notificationLabel.backgroundColor = [UIColor redColor];
- self.wifiOnlyLabel.backgroundColor = [UIColor redColor];
- self.wwanOnlyLabel.backgroundColor = [UIColor redColor];
- return;
- }
- self.notificationLabel.text = @"网络可用";
- self.notificationLabel.backgroundColor = [UIColor greenColor];
- if (reach.isReachableViaWiFi) {
- self.wifiOnlyLabel.backgroundColor = [UIColor greenColor];
- self.wifiOnlyLabel.text = @"当前通过wifi连接";
- } else {
- self.wifiOnlyLabel.backgroundColor = [UIColor redColor];
- self.wifiOnlyLabel.text = @"wifi未开启,不能用";
- }
- if (reach.isReachableViaWWAN) {
- self.wwanOnlyLabel.backgroundColor = [UIColor greenColor];
- self.wwanOnlyLabel.text = @"当前通过2g or 3g连接";
- } else {
- self.wwanOnlyLabel.backgroundColor = [UIColor redColor];
- self.wwanOnlyLabel.text = @"2g or 3g网络未使用";
- }
- }
0 0
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS网络检测Reachability使用
- Reachability,ios检测网络使用环境
- iOS 使用Reachability检测网络状态
- ios检测网络状态Reachability
- iOS Reachability检测网络状态
- IOS使用Reachability实时检测网络连接状况
- ios 开发之使用Reachability检测网络状态
- ios 开发之使用Reachability检测网络状态
- 如何做在线调查问卷,然后群发邮件并分析结果?
- android sensor架构详解
- 第10周项目3血型统计
- python 之 分割参数getopt
- ibatis简例1-用ibator插件自动生成sqlmap
- iOS中使用 Reachability 检测网络
- git 中文乱码问题
- ADB server didn't ACK 解决方法
- 字符串的排列,递归输出
- 2014年ENVI/IDL遥感应用与开发培训班-11月重庆站 开始报名了
- 字符指针数组和字符数组应用
- 从拉普拉斯矩阵说到谱聚类
- js生成0到9,0到10,1到10的随机数
- 第十周项目一 程序填充与阅读3 getchar()