注:本文转载自CocoaChina
前言
小编最近在项目中遇到了一个问题,除iPhone X以外的iOS设备可以正常的搜索到硬件设备,但是iPhone X就不行。因此,小编花了一点时间研究了一下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
实现方法如下:
#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.iPhone X下的网络状态获取
在iPhone X下,小编寻找资料,找了一圈都没有发现可以使小编满意的答案,只找到了一个开源类Reachability,这个类可以获取iPhone X下的网络状态,当然普通设备也可以用。但是在之后,小编根据结构,一步步寻找,终于找到自己想要的东西。
** 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;
}
通过结构获取网络类型
在研究这部分之前,小编在想,为什么在其他设备可以通过遍历状态栏发现网络状态,但是在iPhone X不行。通过打断点一步步摸索发现,iPhone X的状态栏的结果和其他设备不太一样。在分级上复杂了许多,而且移动网络和WIFI竟然是调用了两个完全不太的类。如下:
这让小编感觉到了。。。(自行脑补),在不断的探索下,终于完成了在上面的基础适配iPhone X的网络类型获取。
首先,通过添加宏判断是否是iPhone X,如下:
#define KIsiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
然后通过层级结构判断类型,如下:
+ (NSString *)getNetworkType {
UIApplication *app = [UIApplication sharedApplication];
id statusBar = [app valueForKeyPath:@"statusBar"];
NSString *network = @"";
if (KIsiPhoneX) {
// iPhone X
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")]) {
network = @"WIFI";
}else if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarStringView")]) {
network = [subview valueForKeyPath:@"originalText"];
}
}
}else {
// 非 iPhone X
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依然可用,因此就不多加说明了,获取iPhone X信号强度的方法也类似于上方获取类型。首先判断是否属于WIFI网络,然后根据iPhone X特有的结构找到特定的地方获取值就行,如下:
#pragma mark 获取Wifi信号强度
+ (int)getWifiSignalStrength {
int signalStrength = 0;
// 判断类型是否为WIFI
if ([[self getNetworkType]isEqualToString:@"WIFI"]) {
UIApplication *app = [UIApplication sharedApplication];
id statusBar = [app valueForKey:@"statusBar"];
if (KIsiPhoneX) {
// iPhone X
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")]) {
signalStrength = [[subview valueForKey:@"_numberOfActiveBars"] intValue];
}
}
}else {
// 非 iPhone X
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;
}
到这里为止,这篇文章就结束了,有说明不足的地方欢迎评论,这里附上Demo下载地址:Demo。最后,希望这篇文章对各位看官们有所帮助。