IOS开发网络专属人间不知德-iOS老本行

iOS获取设备的网络状态(刘海屏可用)

2018-01-23  本文已影响1946人  神SKY

前言

小编最近在项目中遇到了一个问题,除刘海屏以外的iOS设备可以正常的搜索到硬件设备,但是刘海屏就不行。因此,小编花了一点时间研究了一下iOS设备获取当前设备的网络状态。

实现

因为iOS的系统是封闭的,所以是没有直接的APi去获取当前的网络状态。但是道高一尺,魔高一尺。开发者总会有办法获取自己想要的东西。

1.一般设备下的网络状态获取

获取当前的网络类型

获取当前的网络类型是通过获取状态栏,然后遍历状态栏的视图完成的。
先导入头文件,如下:

#import "AppDelegate.h"

实现方法如下:

+ (NSString *)getNetworkType
{
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *subviews = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    NSString *network = @"";
    for (id subview in subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            int networkType = [[subview valueForKeyPath:@"dataNetworkType"] intValue];
            switch (networkType) {
                case 0:
                    network = @"NONE";
                    break;
                case 1:
                    network = @"2G";
                    break;
                case 2:
                    network = @"3G";
                    break;
                case 3:
                    network = @"4G";
                    break;
                case 5:
                    network = @"WIFI";
                    break;
                default:
                    break;
            }
        }
    }
    if ([network isEqualToString:@""]) {
        network = @"NO DISPLAY";
    }
    return network;
}

获取当前的Wifi信息

获取当前的Wifi信息需要借助系统的SystemConfiguration这个库。
先导入头文件,如下:

#import <SystemConfiguration/CaptiveNetwork.h>

实现方法如下:

#pragma mark 获取Wifi信息
+ (id)fetchSSIDInfo
{
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        
        if (info && [info count]) {
            break;
        }
    }
    return info;
}
#pragma mark 获取WIFI名字
+ (NSString *)getWifiSSID
{
    return (NSString *)[self fetchSSIDInfo][@"SSID"];
}
#pragma mark 获取WIFI的MAC地址
+ (NSString *)getWifiBSSID
{
    return (NSString *)[self fetchSSIDInfo][@"BSSID"];
}
获取当前的Wifi信号强度

获取信号强度与获取网络状态有点类似,通过遍历状态栏,从而获取WIFI图标的信号强度。在获取前需注意当前状态是否为WIFI。如下:

+ (int)getWifiSignalStrength{
    
    int signalStrength = 0;
//    判断类型是否为WIFI
    if ([[self getNetworkType]isEqualToString:@"WIFI"]) {
    UIApplication *app = [UIApplication sharedApplication];
    id statusBar = [app valueForKey:@"statusBar"];
    UIView *foregroundView = [statusBar valueForKey:@"foregroundView"];
    
    NSArray *subviews = [foregroundView subviews];
    NSString *dataNetworkItemView = nil;
    
    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }
    
    int signalStrength = [[dataNetworkItemView valueForKey:@"_wifiStrengthBars"] intValue];
    }
    return signalStrength;
}

2.刘海屏下的网络状态获取

在刘海屏下,小编寻找资料,找了一圈都没有发现可以使小编满意的答案,只找到了一个开源类Reachability,这个类可以获取刘海屏下的网络状态,当然普通设备也可以用。但是在之后,小编根据结构,一步步寻找,终于找到自己想要的东西。

Reachability的使用

下载开源类Reachability,然后根据文档使用即可(该类把移动网络统称为WWAN):

+ (NSString *)getNetworkTypeByReachability
{
    NSString *network = @"";
    switch ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]) {
        case NotReachable:
            network = @"NONE";
            break;
        case ReachableViaWiFi:
            network = @"WIFI";
            break;
        case ReachableViaWWAN:
            network = @"WWAN";
            break;
        default:
            break;
    }
    if ([network isEqualToString:@""]) {
        network = @"NO DISPLAY";
    }
    return network;
}
通过结构获取网络类型
在研究这部分之前,小编在想,为什么在其他设备可以通过遍历状态栏发现网络状态,但是在刘海屏不行。通过打断点一步步摸索发现,刘海屏的状态栏的结果和其他设备不太一样。在分级上复杂了许多,而且移动网络和WIFI竟然是调用了两个完全不同的类(iOS 11的截图,iOS 11以上版本如有需要,请自己查看)。如下:

这让小编感觉到了。。。(自行脑补),在不断的探索下,终于完成了在上面的基础适配刘海屏的网络类型获取。
首先,通过添加方法判断是否是刘海屏的iPhone,如下:

//刘海屏safeAreaInset的高度(所有的刘海屏都一致)
static const CGFloat liuHaiHeight = 44;
#pragma mark 判断是否是刘海屏
- (BOOL)isLiuHaiScreen
{
    if (@available(iOS 11.0, *)) {
    
        UIEdgeInsets safeAreaInsets = [UIApplication sharedApplication].windows[0].safeAreaInsets;
        
        return safeAreaInsets.top == liuHaiHeight || safeAreaInsets.bottom == liuHaiHeight || safeAreaInsets.left == liuHaiHeight || safeAreaInsets.right == liuHaiHeight;
    }else {
        return NO;
    }
}

然后通过层级结构判断类型,如下:

+ (NSString *)getNetworkType
{
    UIApplication *app = [UIApplication sharedApplication];
    id statusBar = [app valueForKeyPath:@"statusBar"];
    NSString *network = @"";
    
    if ([[[self alloc]init]isLiuHaiScreen]) {
//        刘海屏
        id statusBarView = [statusBar valueForKeyPath:@"statusBar"];
        UIView *foregroundView = [statusBarView valueForKeyPath:@"foregroundView"];
        NSArray *subviews = [[foregroundView subviews][2] subviews];
        
        if (subviews.count == 0) {
//            iOS 12
            id currentData = [statusBarView valueForKeyPath:@"currentData"];
            id wifiEntry = [currentData valueForKey:@"wifiEntry"];
            if ([[wifiEntry valueForKey:@"_enabled"] boolValue]) {
                network = @"WIFI";
            }else {
//                卡1:
                id cellularEntry = [currentData valueForKey:@"cellularEntry"];
//                卡2:
                id secondaryCellularEntry = [currentData valueForKey:@"secondaryCellularEntry"];

                if (([[cellularEntry valueForKey:@"_enabled"] boolValue]|[[secondaryCellularEntry valueForKey:@"_enabled"] boolValue]) == NO) {
//                    无卡情况
                    network = @"NONE";
                }else {
//                    判断卡1还是卡2
                    BOOL isCardOne = [[cellularEntry valueForKey:@"_enabled"] boolValue];
                    int networkType = isCardOne ? [[cellularEntry valueForKey:@"type"] intValue] : [[secondaryCellularEntry valueForKey:@"type"] intValue];
                    switch (networkType) {
                            case 0://无服务
                            network = [NSString stringWithFormat:@"%@-%@", isCardOne ? @"Card 1" : @"Card 2", @"NONE"];
                            break;
                            case 3:
                            network = [NSString stringWithFormat:@"%@-%@", isCardOne ? @"Card 1" : @"Card 2", @"2G/E"];
                            break;
                            case 4:
                            network = [NSString stringWithFormat:@"%@-%@", isCardOne ? @"Card 1" : @"Card 2", @"3G"];
                            break;
                            case 5:
                            network = [NSString stringWithFormat:@"%@-%@", isCardOne ? @"Card 1" : @"Card 2", @"4G"];
                            break;
                        default:
                            break;
                    }
                    
                }
            }
        
        }else {
            
            for (id subview in subviews) {
                if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarWifiSignalView")]) {
                    network = @"WIFI";
                }else if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarStringView")]) {
                    network = [subview valueForKeyPath:@"originalText"];
                }
            }
        }
        
    }else {
//        非刘海屏
        UIView *foregroundView = [statusBar valueForKeyPath:@"foregroundView"];
        NSArray *subviews = [foregroundView subviews];
        
        for (id subview in subviews) {
            if ([subview isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
                int networkType = [[subview valueForKeyPath:@"dataNetworkType"] intValue];
                switch (networkType) {
                    case 0:
                        network = @"NONE";
                        break;
                    case 1:
                        network = @"2G";
                        break;
                    case 2:
                        network = @"3G";
                        break;
                    case 3:
                        network = @"4G";
                        break;
                    case 5:
                        network = @"WIFI";
                        break;
                    default:
                        break;
                }
            }
        }
    }

    if ([network isEqualToString:@""]) {
        network = @"NO DISPLAY";
    }
    return network;
}
通过结构获取WIFI信号强度

因为上面获取SSID和BSSID的方法在iPhone X依然可用,因此就不多加说明了,获取刘海屏信号强度的方法也类似于上方获取类型。首先判断是否属于WIFI网络,然后根据刘海屏特有的结构找到特定的地方获取值就行,如下:

#pragma mark 获取Wifi信号强度
+ (int)getWifiSignalStrength
{
    int signalStrength = 0;
//    判断类型是否为WIFI
    if ([[self getNetworkType]isEqualToString:@"WIFI"]) {
        UIApplication *app = [UIApplication sharedApplication];
        id statusBar = [app valueForKey:@"statusBar"];
        if ([[[self alloc]init]isLiuHaiScreen]) {
//            刘海屏
            id statusBarView = [statusBar valueForKeyPath:@"statusBar"];
            UIView *foregroundView = [statusBarView valueForKeyPath:@"foregroundView"];
            NSArray *subviews = [[foregroundView subviews][2] subviews];
            
            if (subviews.count == 0) {
//                iOS 12
                id currentData = [statusBarView valueForKeyPath:@"currentData"];
                id wifiEntry = [currentData valueForKey:@"wifiEntry"];
                signalStrength = [[wifiEntry valueForKey:@"displayValue"] intValue];
//                dBm
//                int rawValue = [[wifiEntry valueForKey:@"rawValue"] intValue];
            }else {
                
                for (id subview in subviews) {
                    if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarWifiSignalView")]) {
                        signalStrength = [[subview valueForKey:@"_numberOfActiveBars"] intValue];
                    }
                }
            }
        }else {
//            非刘海屏
            UIView *foregroundView = [statusBar valueForKey:@"foregroundView"];
            
            NSArray *subviews = [foregroundView subviews];
            NSString *dataNetworkItemView = nil;
            
            for (id subview in subviews) {
                if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
                    dataNetworkItemView = subview;
                    break;
                }
            }
            
            signalStrength = [[dataNetworkItemView valueForKey:@"_wifiStrengthBars"] intValue];
            
            return signalStrength;
        }
    }
    return signalStrength;
}

上次发布了这篇文章之后,有人问我,怎么才能获取设备的IP地址呢?在这里,小编附上获取iP地址的方法。
先导入头文件,如下:

#import <ifaddrs.h>
#import <arpa/inet.h>

实现方法,如下:

#pragma mark 获取设备IP地址
+ (NSString *)getIPAddress
{
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // 检索当前接口,在成功时,返回0
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // 循环链表的接口
        temp_addr = interfaces;
        while(temp_addr != NULL) {
//                开热点时本机的IP地址
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"bridge100"]
                    ) {
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
                // 检查接口是否en0 wifi连接在iPhone上
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // 得到NSString从C字符串
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // 释放内存
    freeifaddrs(interfaces);
    return address;
}

3.iOS 12下的补充

在iOS 12下xcode需要打开权限才可以正常操作,如下:


到这里为止,这篇文章就结束了。在这里提醒一下各位看官,横屏时请注意不要把状态栏去掉。有说明不足的地方欢迎评论,这里附上Demo下载地址:Demo。最后,希望这篇文章对各位看官们有所帮助。

上一篇下一篇

猜你喜欢

热点阅读