美文网首页iOS开发记录iOS开发-网络需要读的资料
iPhoneX/Xs/XR/Xs Max通过状态栏判断网络状态

iPhoneX/Xs/XR/Xs Max通过状态栏判断网络状态

作者: DSA碼侬 | 来源:发表于2018-08-09 19:48 被阅读2608次

    判断网络情况 这里没有用AFN判断,下文方法是通过扫描状态栏的显示情况进行判断的:

    一、之前(非刘海屏)的用法:

    1、首先定义网络类型为枚举类型:
    // 网络类别枚举类型
    typedef NS_ENUM(NSUInteger, NetWorkType) {
        
        NetWorkTypeNone = 0,         // 无网
        
        NetWorkType2G = 1,           // 2G
        
        NetWorkType3G = 2,          // 3G
        
        NetWorkType4G = 3,          // 4G
        
        NetWorkTypeWiFI = 5,       // wifi
        
    };
    
    2、自定义方法,(通过遍历手机状态栏的显示)求得网络类型:
    + (NetWorkType)currentNetworkType{
        // 1、求出状态栏的子视图数组
        NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
        
        // 2、遍历子视图求出网络类型
        NetWorkType type;
        for (id info in infoArray){
            if ([info isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
                type = [[info valueForKeyPath:@"dataNetworkType"] integerValue];
                
                return (NetWorkType)type;  // 0 : 无网; 1 : 2G网; 2 : 3G网; 3 : 4G网;5 : wifi网;
            }
        }
        return NetWorkTypeNone;
    }
    

    注意:利用这种遍历的方法在iPhoneX/Xs/XR/Xs Max 机型上运行是崩溃,其实根本原因是手机刘海儿的状态栏子视图发生了变化,用以下方法便可解决。

    二、刘海屏的用法:

    + (NetWorkType)currentNetworkType{
        
    //  iPhoneX、iPhoneXR、iPhoneXS_Max是宏定义 (iPhoneX == iPhoneXs)
        if (iPhoneX || iPhoneXR || iPhoneXS_Max) { 
    
            UIApplication *app = [UIApplication sharedApplication];
            id statusBar = [app valueForKeyPath:@"statusBar"];
            id statusBarView = [statusBar valueForKeyPath:@"statusBar"];
            UIView *foregroundView = [statusBarView valueForKeyPath:@"foregroundView"];
            
            NSArray *subviews = [[foregroundView subviews][2] subviews];
            
            for (id subview in subviews) {
    
                if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarWifiSignalView")]) {
    
                    return NetWorkTypeWiFI;  // wifi网
                }else if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarStringView")]) {
    
                    NSString *str = [subview valueForKeyPath:@"_originalText"]; // @"text" @"originalText"一样
                    if ([str isEqualToString:@"4G"]) {
    
                        return NetWorkType4G;  // 4G网
                    }else if([str isEqualToString:@"3G"]){
    
                        return NetWorkType3G; // 3G网
                    }else{
    
                        return NetWorkType2G; // 2G网
                    }
                }
            }
            return NetWorkTypeNone;  // 无网络
        }
    }
    

    另:关于刘海儿的视图分层如下图所示(手机是4G),结果获得的就是4G网络

    iPhoneX状态栏

    注意:我们用的这种方法提交之后,审核被拒,说是在iOS 12上,崩溃,通过解析苹果返回的崩溃日志,可得知,有的设备key-value键值对是不存在的,截图如下所示:

    崩溃日志

    解决办法:使用苹果自带的:Reachability

    第一步:从Demo(左上角)中,把Reachability.h & Reachability.m文件拷贝到项目里

    第二步:代码实现

    + (NetWorkType)currentNetworkType{
        
        Reachability *reachability   = [Reachability reachabilityWithHostName:@"www.apple.com"];
        NetworkStatus internetStatus = [reachability currentReachabilityStatus];
       
        NetWorkType net = NetWorkTypeNone;
        switch (internetStatus) {
            case ReachableViaWiFi:
                net = NetWorkTypeWiFI;
                break;
                
            case ReachableViaWWAN:
                net = NetWorkType4G;
    //            net = [self getNetType];   //判断具体类型
                break;
                
            case NotReachable:
                net = NetWorkTypeNone;
                
            default:
                break;
        }
        
        return net;
        
    }
    + (NSString *)getNetType
    {
        CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
        NSString *currentStatus = info.currentRadioAccessTechnology;
        NSString *netconnType;
        if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"]) {
            netconnType = @"GPRS";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyEdge"]) {
            netconnType = @"2.75G EDGE";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"]){
            netconnType = @"3G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSDPA"]){
            netconnType = @"3.5G HSDPA";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSUPA"]){
            netconnType = @"3.5G HSUPA";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"]){
            netconnType = @"2G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"]){
            netconnType = @"3G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"]){
            netconnType = @"3G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"]){
            netconnType = @"3G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyeHRPD"]){
            netconnType = @"HRPD";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"]){
            netconnType = @"4G";
        }
        return netconnType;
    }
    
    

    over!

    相关文章

      网友评论

        本文标题:iPhoneX/Xs/XR/Xs Max通过状态栏判断网络状态

        本文链接:https://www.haomeiwen.com/subject/cqbcbftx.html