iOS--另辟蹊径监听网络类型及状态

对App进行实时的网络状态监听也是项目中的一项比较重要的功能.
在写项目的时候也查阅了很多资料,基本就两种方法:Reachability和AFNetworking两种方法.
这两种方法的具体实现方法在上都有很多很不错的文章讲过
但这篇文章我想讲讲查到的另一个判断网络类型的方法---通过状态栏判断网络类型

我的项目内采用的是AFNetworkReachabilityManager,它的底层代码就是对系统的Reachability的二次封装,使用起来能更方便些.话不多说了,看一下具体的实现

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status)
            {
                //结合MBProgressHUD进行显示:
                case -1:  //AFNetworkReachabilityStatusUnknown
                {
                    //提示框:
                    MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                    textOnlyHUD.mode = MBProgressHUDModeText;
                    textOnlyHUD.labelText = @"未知网络";
                    [textOnlyHUD hide:YES afterDelay:1.f];
                }
                    break;
                case 0:  //AFNetworkReachabilityStatusNotReachable
                {
                    //提示框:
                    MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                    textOnlyHUD.mode = MBProgressHUDModeText;
                    textOnlyHUD.labelText = @"无法连接";
                    [textOnlyHUD hide:YES afterDelay:1.f];
                }
                    break;
                case 1:  //AFNetworkReachabilityStatusReachableViaWWAN
                {
                    //这里是本文的核心点:采用遍历查找状态栏的显示网络状态的子视图,通过判断该子视图的类型来更详细的判断网络类型
                    NSArray *subviewArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
                    int type = 0;
                    for (id subview in subviewArray)
                    {
                        if ([subview isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")])
                        {
                            type = [[subview valueForKeyPath:@"dataNetworkType"] intValue];
                    }
                    switch (type)
                    {
                        case 1:
                        {
                            //提示框:
                            MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                            textOnlyHUD.mode = MBProgressHUDModeText;
                            textOnlyHUD.labelText = @"当前为2G网络";
                            [textOnlyHUD hide:YES afterDelay:1.f];
                        }
                            break;
                        case 2:
                        {
                            //提示框:
                            MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                            textOnlyHUD.mode = MBProgressHUDModeText;
                            textOnlyHUD.labelText = @"当前为3G网络";
                            [textOnlyHUD hide:YES afterDelay:1.f];
                        }
                            break;
                        case 3:
                        {
                            //提示框:
                            MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                            textOnlyHUD.mode = MBProgressHUDModeText;
                            textOnlyHUD.labelText = @"当前为4G网络";
                            [textOnlyHUD hide:YES afterDelay:1.f];
                        }
                            break;
                            
                        default:
                            break;
                    }
                }
            }
                break;
            case 2:  //AFNetworkReachabilityStatusReachableViaWiFi
            {
                //提示框:
                MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                textOnlyHUD.mode = MBProgressHUDModeText;
                textOnlyHUD.labelText = @"当前为WIFI";
                [textOnlyHUD hide:YES afterDelay:1.f];
            }
                break;
                
            default:
            {
                //提示框:
                MBProgressHUD *textOnlyHUD = [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
                textOnlyHUD.mode = MBProgressHUDModeText;
                textOnlyHUD.labelText = @"无网络连接";
                [textOnlyHUD hide:YES afterDelay:1.f];
            }
                break;
        }
    }];
    
    //开始监测
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];

最后的实现效果:

iOS--另辟蹊径监听网络类型及状态_第1张图片
WIFI

iOS--另辟蹊径监听网络类型及状态_第2张图片
3G

此方法最大的好处就是能够更详细的监听网络类型,能够和用户的视觉信息一致,会有更好的用户体验.(个人感觉)希望能够给大家一点收获,我觉得拓宽思路,这种方法还能有更高的使用价值
(ps:如有任何问题,欢迎随时找我交流,共同进步)

你可能感兴趣的:(iOS--另辟蹊径监听网络类型及状态)