RealReachability的使用心得

时间:2022-10-19 16:36:22
//关于reachability的优化版本

一. 关于目前reachability的缺点

1.现在很流行的公用wifi,需要网页鉴权,鉴权之前无法上网,但本地连接已经建立;

2.存在了本地网络连接,但信号很差,实际无法连接到服务器;

3.iOS连接的路由设备本身没有连接外网。

原因: Reachability相关的框架在底层都是通过SCNetworkReachability来实现网络检测的,所以无法检测实际网络连接情况。

二. 优化方法(RealReachability)
链接: http://www.cocoachina.com/ios/20160224/15407.html

1.集成: pod 'RealReachability'

2.常用代码:

* 实时监控网络连接状态的方法:

[GLobalRealReachability startNotifier];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:@"kRealReachabilityChangedNotification" object:nil];

- (void)networkChanged:(NSNotification *)notification
{
RealReachability *reachability = (RealReachability *)notification.object;
ReachabilityStatus status = [reachability currentReachabilityStatus];

if (status == NotReachable) {
self.curStatusLabel.text = @"没有网络";
}
if (status == ReachableViaWiFi) {
self.curStatusLabel.text = @"WiFi网络";
}
if (status == ReachableViaWWAN) {
self.curStatusLabel.text = @"3/4G网络";
}
}

* 手动检测网络的方法

[GLobalRealReachability startNotifier];

- (IBAction)checkTheStatus:(id)sender {

[GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) {
switch (status)
{
case NotReachable:
{
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"没有网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
break;
}

case ReachableViaWiFi:
{
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"WIFI" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
break;
}

case ReachableViaWWAN:
{
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"3G/4G" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
break;
}
default:
break;
}
}];
}