iOS - Reachability 日志框架源码分析

1. 功能介绍

1.1 Reachability

Reachablity 是一个 iOS 下检测,iOS 设备网络环境用的库。

  • 监视目标网络是否可用
  • 监视当前网络的连接方式
  • 监测连接方式的变更

苹果官方提供的 Doc:链接

1.2 Reachability 的安装

(1).创建 network 工程(network是我创建的demo工程,附件中可以下载到)
(2).使用Cocoaspod安装依赖
在项目中添加 SystemConfiguration.framework 库
由于Reachability非常常用。直接将其加入到Supporting Files/networ-Prefix.pch中:

2. 使用

2.1 Block 的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
- (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];
}

2.2 通知的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
- (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网络未使用";
}
}
------本文结束 感谢阅读------

本文地址:http://kaaaaai.cn/articles/047.html
本文基于 知识共享署名-相同方式共享 4.0 国际许可协议发布
转载请注明出处,谢谢!

众筹项目:拯救世界!
0%