首页 > 其他 > 详细

[翻译] JTSReachability

时间:2015-02-05 21:52:17      阅读:312      评论:0      收藏:0      [点我收藏+]

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都是在主线程上面执行的,所以,也必须在主线程上面移除掉。

 

[翻译] JTSReachability

原文:http://www.cnblogs.com/YouXianMing/p/4273960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!