iOS中使用Reachability 检测网络

简介:
如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote)。那么你会使用到Reachability来实现网络检测。
   写本文的目的
  了解Reachability都能做什么
  检测3中网络环境
  2G/3G
  wifi
  无网络
  如何使用通知
  单个controller
  多个controller
  简单的功能:
  仅在wifi下使用
   Reachability简介
  Reachablity 是一个iOS下检测,iOS设备网络环境用的库。
  监视目标网络是否可用
  监视当前网络的连接方式
  监测连接方式的变更
  安装
  创建 network 工程(network是我创建的demo工程,附件中可以下载到)
  使用Cocoaspod安装依赖
  在项目中添加 SystemConfiguration.framework 库
  由于Reachability非常常用。直接将其加入到Supporting Files/networ-Prefix.pch中:
  #import <Reachability/Reachability.h>
  #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方式使用
- (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];
}
- (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的方式
- (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网络未使用";
}
}
- (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网络未使用";
}
}
  附件demo说明
  开启wifi状态
  关闭wifi的状态
  遗留问题
  如何在多个controller之前共用一个Reachability(附件demo中是一个controller一个Reachability实例)
  应该在什么使用停止Reachability的检测.

最新内容请见作者的GitHub页:http://qaseven.github.io/
相关文章
|
17天前
|
数据可视化 数据挖掘
【视频】复杂网络分析CNA简介与R语言对婚礼数据聚类社区检测和可视化|数据分享
【视频】复杂网络分析CNA简介与R语言对婚礼数据聚类社区检测和可视化|数据分享
|
2天前
|
算法
计算机网络:封装成帧 & 透明传输 & 差错检测
计算机网络:封装成帧 & 透明传输 & 差错检测
8 0
|
2天前
|
Android开发
android检测网络连接是否存在(一)
android检测网络连接是否存在(一)
|
6天前
|
机器学习/深度学习 存储 算法
m基于Yolov2深度学习网络的螺丝检测系统matlab仿真,带GUI界面
MATLAB 2022a中展示了YOLOv2算法的螺丝检测仿真结果,该系统基于深度学习的YOLOv2网络,有效检测和定位图像中的螺丝。YOLOv2通过批标准化、高分辨率分类器等优化实现速度和精度提升。核心代码部分涉及设置训练和测试数据,调整图像大小,加载预训练模型,构建YOLOv2网络并进行训练,最终保存检测器模型。
23 3
|
8天前
|
机器学习/深度学习 人工智能 安全
【AI 初识】人工智能如何用于欺诈检测和网络安全?
【5月更文挑战第3天】【AI 初识】人工智能如何用于欺诈检测和网络安全?
|
16天前
|
机器学习/深度学习 算法 计算机视觉
m基于Yolov2深度学习网络的人体喝水行为视频检测系统matlab仿真,带GUI界面
MATLAB 2022a中使用YOLOv2算法对avi视频进行人体喝水行为检测,结果显示成功检测到目标。该算法基于全卷积网络,通过特征提取、锚框和损失函数优化实现。程序首先打乱并分割数据集,利用预训练的ResNet-50和YOLOv2网络结构进行训练,最后保存模型。
28 5
|
18天前
|
算法 数据可视化 数据挖掘
R语言社区发现算法检测心理学复杂网络:spinglass、探索性图分析walktrap算法与可视化
R语言社区发现算法检测心理学复杂网络:spinglass、探索性图分析walktrap算法与可视化
|
13天前
|
监控 安全 Linux
【专栏】Linux中六个常用的网络命令:ping、traceroute、netstat、nmap、ifconfig和ip
【4月更文挑战第28天】本文介绍了Linux中六个常用的网络命令:ping、traceroute、netstat、nmap、ifconfig和ip,以及它们在测试网络连通性、追踪路由、查看网络状态、安全扫描和接口配置等场景的应用。通过学习和运用这些命令,系统管理员和网络爱好者能更有效地诊断和管理网络问题,确保网络稳定运行。
|
3天前
|
域名解析 网络协议 Linux
linux网络配置详解
linux网络配置详解
13 0
|
4天前
|
网络协议 Java Linux
【探索Linux】P.29(网络编程套接字 —— 简单的TCP网络程序模拟实现)
【探索Linux】P.29(网络编程套接字 —— 简单的TCP网络程序模拟实现)
11 0