[翻译] JTSReachability

简介:

JTSReachabilit 

 

An adaptation of Apple's Reachability with some block-based conveniences.

这是一个苹果的网络检测类的改编版本,提供便利的基于block的方法。

 

Usage

Usage is straightforward.

使用就如其名一样简单。

 

Singleton

Access the singleton instance of JTSReachabilityResponder via:

你可以通过以下方法来操作单例:

+ (instancetype)sharedInstance;

 

Or Non-Singleton, if You're Not Into the Whole Singleton Thing

Instantiate an instance of JTSReachabilityResponder via:

你也可以根据某个具体的hostname来作为网络检测的基准:

- (instancytype)initWithOptionalHostname:(NSString *)hostname;

If you don't specify a hostname, there are some edge cases where Apple's networking frameworks will assume there's a valid connection even when there isn't (e.g. when connected to a local WiFi network without a connection to the Internet proper).

如果你没有指定一个hostname,会有一些极端情况导致出错(比方说,你连上了路由器上的wifi,但是这个路由器没有连上网,这个时候你是不能上网的)

 

Registering Status Change Handlers

An object in your application registers a block to be executed whenever the network status changes between one of the three known states (none, Wi-fi, or cellular) as follows:

个block对象会因为网络状态的改变而执行(三种状态:没有网络,wifi,cellular)

- (void)someSetupMethod {

    JTSReachabilityResponder *responder = [JTSReachabilityResponder sharedInstance];

    [responder addHandler:^(JTSNetworkStatus status) {
        // Respond to the value of "status"
    } forKey:@"MyReachabilityKey"]; } 

Your object is responsible for cleaning up after itself, typically in dealloc, as follows:

你也需要在dealloc方法中将这个监听移除掉哦:

- (void)dealloc {

    JTSReachabilityResponder *responder = [JTSReachabilityResponder sharedInstance];

    [responder removeHandlerForKey:@"MyReachabilityKey"]; }

All blocks are called on the main thread, and must be added or removed on the main thread.

所有的block都是在主线程上面执行的,所以,也必须在主线程上面移除掉。

 

目录
相关文章
|
数据可视化 物联网 Python
GNU Radio简介及流程图搭建
GNU Radio简介及流程图搭建
547 0
|
Web App开发 JavaScript 前端开发
XSS-Labs 通过解析(下)
XSS-Labs 通过解析(下)
|
文字识别 算法 API
视觉智能开放平台产品使用合集之如何批量添加人脸数据
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
160 1
|
JSON Java 数据格式
springboot全局异常实现以及@Valid和@Validated优雅实现入参验证
springboot全局异常实现以及@Valid和@Validated优雅实现入参验证
1315 0
|
开发工具 git
Source Tree多个远程仓库管理及如何在gitlab提Merge
目录 一、背景 二、Gitlab上Fork仓库 三、SoureTree上新增远程仓库 1、进入仓库设置 2、新增远程仓库 四、同步原仓库代码以及gitlab提merge 1、同步原仓库最新代码 2、gitlab上提merge
Source Tree多个远程仓库管理及如何在gitlab提Merge
|
IDE Java 开发工具
Lombok入门使用教程及其优缺点详解(下)
Lombok入门使用教程及其优缺点详解
|
Oracle 关系型数据库 Shell
linux下的文件系统出错 Structure needs cleaning
[oracle@localhost ~]$ /u01/app/oracle/product/version/db_1/bin/sqlplus -bash: /u01/app/oracle/product/version/db_1/bin/sqlplus: Structure needs cleaning [oracle@localhost ~]$
443 0
|
前端开发 JavaScript 小程序
预备金九银十,这套前端面试小册阁下请收好
预备金九银十,这套前端面试小册阁下请收好
213 0
|
Java 数据处理 调度
浅谈Java中的“八锁”——概述、常见实现方式与使用场景
浅谈Java中的“八锁”——概述、常见实现方式与使用场景
508 0
|
Ubuntu Python
修改ubuntu上的pycharm镜像源,详细步骤
修改ubuntu上的pycharm镜像源,详细步骤
676 0
修改ubuntu上的pycharm镜像源,详细步骤