Reachability

掌握iOS网络可达性检测:Reachability库使用手册 在移动应用开发中,网络状态的实时监测是确保用户良好体验的关键因素之一。Reachability库作为iOS开发者常用的网络状态检测工具,已经成为项目中不可或缺的一部分。本章节将介绍Reachability库的基本概念、特点以及其在网络通信中的重要性,为接下来深入探讨其功能和应用打下坚实基础。Reachability库是什么?Reachability是一个由开发者维护的开源项目,旨在提供一个简单而强大的API,使开发者能够轻松监测网络可达性状态的变化。 阅读详情

Even though Apple provides sample code for monitoring changes in network availability, Apple's Reachability class is a bit outdated and doesn't support ARC (Automatic Reference Counting). If you use AFNetworking, then you may want to consider AFHTTPClient, which also lets you monitor network interface changes.

However, my preferred solution is the wonderful Reachability class created and maintained by Tony Million. It supports ARC and leverages GCD (Grand Central Dispatch). Let me show you how to integrate Tony's Reachability class.

Create a new project in Xcode by selecting the Single View Application template from the list of templates (figure 1). Name your application Reachability, enter a company identifier, set iPhone for the device family, and check Use Automatic Reference Counting. The rest of the checkboxes can be left unchecked for this project (figure 2). Tell Xcode where you want to save the project and click Create.

iOS Quick Tip: Detecting Network Changes with Reachability - Choosing a Project Template
Figure 1: Choosing a Project Template iOS Quick Tip: Detecting Network Changes with Reachability - Configuring the Project
Figure 2: Configuring the Project

Integrating Tony Million's Reachability class is trivial. Visit the project's GitHub page, download the latest version, and drag Reachability.h/.m into your Xcode project (figure 3). If you take this path, make sure to copy the class files into your Xcode project (figure 4). You can also use CocoaPods to to add Reachability to your project.

iOS Quick Tip: Detecting Network Changes with Reachability - Adding the Reachability Class to Your Project
Figure 3: Adding the Reachability Class to Your Project iOS Quick Tip: Detecting Network Changes with Reachability - Make Sure to Copy the Class Files into Your Project
Figure 4: Make Sure to Copy the Class Files into Your Project

The Reachability class depends on the System Configuration framework for some of its functionality. With your project selected in the Project Navigator, select the Reachability target in the list of targets, open the Build Phases tab, and expand the Link Binary with Libraries drawer. Click the plus button and search for SystemConfiguration.framework (figure 5).

iOS Quick Tip: Detecting Network Changes with Reachability - Link Your Project Against the System Configuration Framework
Figure 5: Link Your Project Against the System Configuration Framework

The Reachability class provides two ways to monitor changes in network availability: blocks and notifications. Let me show you how this works.

To keep things simple, we will add the reachability logic to the application delegate. Start by adding an import statement for Reachability.h to MTAppDelegate.m as shown below.

We first need to create an instance of the Reachability class in application:didFinishLaunchingWithOptions: (MTAppDelegate.m) as shown below. We then set the reachableBlock and unreachableBlock on the Reachability instance. The reachableBlock is invoked when the network availability changes from unreachable to reachable. The reverse is true for the unreachableBlock. It is key to send the Reachability instance a message of startNotifier so that it knows that it should start monitoring for reachability changes. It is also important to know that the call to startNotifier causes the Reachability instance to retain itself, which means that you don't need to store a reference to the Reachability object. However, I am not a fan of this approach and will show you an alternative solution a bit later.

Before we take a look at notifications, I need to stress that the reachable and unreachable blocks are invoked on a background thread. Keep this in mind when you need to update your application's user interface when the network interface changes.

The advantage of using notifications for reachability changes is that any object in your application can register itself as an observer for these notifications. In contrast to the use of blocks, the reachability notifications are posted and delivered on the main thread. As you can see below, in application:didFinishLaunchingWithOptions:, we create an instance of the Reachability class and tell it to start monitoring for network changes.

As an example, if the view controller needs to be informed of changes in network availability, then we need to add it as an observer of the notifications the Reachability instance posts. Open MTViewController.m, add an import statement for Reachability.h, and update initWithNibName:bundle: as shown below.

Every time the network interface changes, reachabilityDidChange: is invoked and the view controller can respond appropriately. The object property of the notification is the Reachability instance that posted the notification. The Reachability class provides a number of useful instance methods, such as isReachable, isReachableViaWWAN, and isReachableViaWiFi. You can even tell the Reachability instance whether it should consider WWAN as unreachable by setting the reachableOnWWAN property accordingly.

For applications that require a network connection, I usually use an alternative approach for managing reachability. I create a separate class named ReachabilityManager that adopts the singleton pattern. The singleton object manages a Reachability instance and provides a number of useful class methods. Let me walk you through the internals of this class.

As I mentioned, the MTReachabilityManager class adopts the singleton pattern and it provides access to the singleton object through the sharedManager class method. This is useful if an object needs direct access to the reachabilityinstance that the singleton object manages.

Through a number of class methods objects can ask the reachability manager about the current network interface. In addition, objects can still add themselves as observers for the kReachabilityChangedNotification notifications as we saw earlier.

The implementation of MTReachabilityManager isn't too surprising. If you are unfamiliar with the singleton pattern, then the implementation of sharedManagermight seem a bit odd. Even though the MTReachabilityManager class makes use of the singleton pattern, it is technically possible to create instances of the class. We could prevent this by checking the value of _sharedManager in the init, but I assume that whoever uses the MTReachabilityManager class has taken a look at the class's interface file revealing that it adopts the singleton pattern.

If you decide to adopt this alternative approach and use a reachability manager that manages an instance of the Reachability class, then don't forget to instantiate the singleton object when your application launches. You can do so by calling sharedManager on the MTReachabilityManager class.

Notifying the user or updating the application's user interface when the network interface changes not only results in a better user experience, Apple requires that you do this if your application relies on network connectivity. It does take some effort, but the Reachability class makes it a lot easier.

Reachability网络状态 iOS中获取当前网络连接状态,iOS11中不能再使用导航栏中的View通过KVC的方式获取,使用该工具可以简单实现获取当前网络状态,无网络/WIFI/数据网 立即下载

相关推荐

使用Reachability类判断iOS设备的当前网络连接类型

主要介绍了使用Reachability类判断iOS设备的当前网络连接类型,这里开发语言为传统的Objectice-C,需要的朋友可以参考下

iOS网络4——Reachability检测网络状态

一、整体介绍 前面已经介绍了网络访问的NSURLSession、NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发。 其实在网络开发中还有比较常用的就是网络状态的检测。苹果对需要联网的应用要求很高,就是必须要进行联网检查。另外,当网络发生异常时能够及时提示用户网络已断开,而不是程序问题造成卡顿;当用户观看视频或下载大文件时,提示用户当前的网络...

weixin_30387339的博客 183

DDR核心机制解析:从Burst到Prefetch的性能跃迁

本文深入解析了DDR内存性能跃迁的核心机制,重点阐述了Burst(突发传输)与Prefetch(预取)技术的协同工作原理。Burst机制通过连续传输数据减少访问延迟,而Prefetch则通过内部预取数据提升吞吐量,两者结合完美匹配现代CPU缓存行需求,是DDR3等内存实现高效带宽的关键。

weixin_29232121的博客 135

iOS开发-Reachability网络状态

iOS开发-Reachability网络状态Reachablity 是一个iOS下检测,iOS设备网络环境用的库。

一名开发者 1231

Etcd教程 — 第三章 Etcd集群动态发现、DNS发现

Etcd教程 — 第三章 Etcd集群动态发现、DNS发现

Mr_XiMu的博客 2819

Windows下Python安装geopandas全攻略:解决GDAL依赖问题的5个关键步骤

本文详细解析了在Windows系统下安装Python地理空间分析库geopandas时,如何有效解决其核心依赖GDAL的编译难题。文章提供了五个关键步骤,包括理解依赖关系、获取预编译wheel文件、顺序安装及环境验证,旨在帮助用户绕过复杂的C++编译环境,顺利完成geopandas的安装部署。

weixin_28221501的博客 283

多语言货运管理系统开发实战与架构解析

多语言处理技术在现代物流系统中扮演着关键角色,其核心原理是通过自然语言处理(NLP)实现跨语言文本的自动转换与校验。结合动态语料库和智能纠错算法,这类系统能显著提升跨境贸易文档的处理效率。从技术实现来看,Python+PostgreSQL的技术栈配合分布式任务队列,既能满足文本解析需求,又能保证系统扩展性。在实际应用中,多语言货运管理系统(如ML-FMS)通过混合翻译策略(规则引擎+机器学习)和物流状态机设计,有效解决了报关错误和物流信息不对称等痛点,特别适用于东南亚等多语言地区的跨境电商场景。系统实测显示

weixin_34187862的博客 400

GDSFactory环境配置实战:Python、KLayout与PDK协同指南

GDSFactory是面向光子集成电路(PIC)设计的Python参数化建模框架,其核心价值在于将版图生成从手动绘图升维为可编程、可复现、可集成CI/CD的工程实践。它依赖Python科学计算生态(如NumPy)、EDA可视化工具KLayout的深度Python绑定,以及工艺厂PDK的标准化接入,三者构成‘计算-几何-工艺’技术闭环。典型应用场景包括教学级SiEPIC PDK快速原型、工业级AIM Photonics流片准备,以及自动化DRC与光学仿真流水线构建。本文聚焦GDSFactory落地中最关键也最

weixin_30983353的博客 368

iOS网络状态监测之Reachability的使用

1.Reachability简介 Reachability是一个在iOS系统环境下检测iOS设备当前的网络环境的库。它的主要功能是监测三种网络环境:2G/3G/4G、WiFi网络、无网络。在应用的运行状态下,实时监测网络连接方式的变更,及时给出通知。 2.Reachability的使用 首先需要在GitHub上下载Reachability的压缩包,解压后将Reachabili

magical_code的博客 1448

(持续更新中!)详解【计算机类&面试真题】军队文职考试 ——第二期(真题+解析)| 网络协议的三个核心要素;交互式系统中的非剥夺策略;中断和陷入的区别;可变分区管理中的硬件机制;数据库系统的优点

本篇博客将讨论军队文职面试(计算机类)的几个关键问题。我们将介绍网络协议的核心要素(帮助你理解网络通信的基本原理)、交互式系统中的非剥夺策略(解释为何这是一个优秀的策略以及其对用户体验、系统稳定性和可预测性的影响)、中断和陷入的区别(从引起原因、处理机制、权限级别和应用等方面进行阐述)、可变分区管理的硬件机制以及数据库系统与文件系统的优点比较。通过阅读本文,您将深入了解这些话题,为面试准备和实际应用提供有价值的知识。

追光者♂:记录、分享、总结、提升,现象级专栏《Python从入门到人工智能》作者,无惧黑暗,坚信曙光 1706

一个非常简单的变分量子分类器 (VQC)

​ 在之前的帖子(这里和这里)中,我已经开始谈论 QML,为什么以及如何学习,从现在开始,我将开始分享我的研究和发现,到目前为止,这些都是非常基本的。​

gongdiwudu的专栏 6208

Reachability使用简介

Reachability使用简介

专注移动设备开发 2102

紫光展锐面试——FPGA/IC设计面试面经案例总结

自我介绍;研究生的主要研究方向是什么?为什么想要应聘数字芯片相关的工作?简单介绍下数字IC设计流程。逻辑综合主要做什么?你有实际做过逻辑综合工作吗?你了解SS corner和FF corner吗?综合时应该使用哪个corner?综合时需要考虑hold吗?为什么?提高时钟频率对setup和hold有什么影响?为什么?hvt、rvt/svt、lvt有什么区别?c40、c50是指什么概念?有什么区别?你对EDA工具有过了解吗?

FPGA/MATLAB学习教程/源码/项目合作开发 1561

Reachability的使用相关问题

最近在做东西的时候,遇到收通知很慢的问题,后来才现是使用不当。 1.版本问题 使用2.0之前及之后,不太一样。2.0前的没用过,就不多说了,看下面文章即可。 判断是否打开WiFi以及是否打开3G的用法还真没见过。 2.用法问题 用法的话,开始启动时应该就有一个值,当前网络的状态,通知与首次无关。 以下为转 [iOS]网络编程专题:Reachability的使用 博客

Saick的专栏 805

SIWAVE+ADS提取PCB走线寄生参数

本文将介绍一种很适合PCB走线提取寄生电感等参数的方法,即是通过SIWAVE将PCB走线的S参数提取出来,再通过ADS将S参数转换成电感、电容值。

sinat_15150363的博客 1764

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Reachability 项目使用教程

Reachability 项目使用教程 1. 项目的目录结构及介绍 Reachability/ ├── README.md ├── Reachability.swift ├── Example/ │ ├── ReachabilityExample/ │ │ ├── AppDelegate.swift │ │ ├── Info.plist │ │ ├── ViewCont...

gitblog_01106的博客 948

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

告别Apple原生Reachability:Reachability.swift的革命性改进

Reachability.swift是一款用Swift重写的网络可达性检测库,作为Apple原生Reachability的完美替代品,它采用闭包设计提供更现代、更简洁的网络状态检测方案。无论是iOS、macOS还是tvOS应用,都能通过这个轻量级库轻松实现网络状态监听,让开发者告别繁琐的通知中心代码,专注于业务逻辑实现。 ## 🌟 核心优势:为何选择Reachability.swift? #

gitblog_00709的博客 965

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

iPhone网络编程之--Reachability

一:确认网络环境3G/WIFI    1. 添加源文件和framework        开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审(我们的)查的。    Apple 的 例程 Reachability 中介绍了取

无限互联@彪哥 2721

Swift Reachability

Reachability是苹果提供的用于检查网络状态的. 使用: 导入包SystemConfiguration.framework. 创建Reachability类, 到苹果官网下载相应的代码. 地址:Reachability/Reachability.h/Reachability.m 在需要的地方导入头文件Reachability.h即可. 具体使用可以百度谷歌都行. Swi...

燕慕鸿终 1804

利用Reachability框架进行网络连接状态监测

网络连接状态监测几乎任何地方都离不了。进行联网状态监测,最简单的方法是使用第三方框架Reachability。其实就两个文件Reachability.h和Reachability.m文件,然后导入头文件就可以使用了。 打开Reachability.h文件。最常使用的是 + (instancetype)reachabilityWithHostName:(NSString *)hostName

664

关于官方Reachability Demo理解

1. 示例说明:此示例用于演示关于如何使用SystemConfiguratio Reachalility程序接口来判定相关的网络连接状态及状态变化,所以在使用此示例中的Reachability.(h/m)文件于自有的项目中的前提是必须引入SystemConfiguration.framework。 2. 首选分析Reachability.h文件: -> 首先引入&...

diaoguan9600的博客 123

iOS - 检测网络状态: Reachability和非Reachability

Reachability:转自: http://stackoverflow.com/questions/5195012/how-to-use-reachability-class-to-detect-valid-internet-connectioncode:前提:添加框架SystemConfiguration.framework 苹果官方提供了一个叫Reachability的示例程序,便于开发者

极客学伟的技术分享社区 2385
上一篇: IOS开发系列--IOS程序开发概览(版本稍微老了一些)
下一篇: NSRunLoop
iOSZhong
博客等级 码龄11年 1粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值