方法一:使用NetworkExtension/NEHotspotHelper来获取WIFI信息。但是得向苹果申请权限,比较麻烦
方法二:通过获取状态栏,然后遍历状态栏的视图
1.因为iPhoneX机型以上UIStatusBar 和 iPhoneX机型以下UIStatusBar的视图结构是不同的,所以先判断机型
+ (BOOL)isIponeXLaterDevice{
UIApplication *app = [UIApplication sharedApplication];
id statusBar = [app valueForKeyPath:@"_statusBar"];
if([statusBar isKindOfClass:
NSClassFromString(@"UIStatusBar_Modern")]){
return YES;
}else{
return NO;
}
}
2.通过结构获取网络类型
+ (NSString *)getNetworkType
{
UIApplication *app = [UIApplication sharedApplication];
id statusBar = [app valueForKeyPath:@"statusBar"];
NSString *network = @"";
if ([self isIponeXLaterDevice]) {
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;
}
3.获取信号强度(WIFI和蜂窝网络)
+ (int)getSignalStrength{
int signalStrength = 0;
//判断类型是否为WIFI
if ([[self getNetworkType]isEqualToString:@"WIFI"]) {
UIApplication *app = [UIApplication sharedApplication];
id statusBar = [app valueForKey:@"statusBar"];
if ([self isIponeXLaterDevice]) {
// 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];
break;
}else if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarStringView")]) {
signalStrength = [[subview valueForKey:@"numberOfActiveBars"] intValue];
break;
}
}
}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]] && [[self getNetworkType] isEqualToString:@"WIFI"] && ![[self getNetworkType] isEqualToString:@"NONE"]) {
dataNetworkItemView = subview;
signalStrength = [[dataNetworkItemView valueForKey:@"_wifiStrengthBars"] intValue];
break;
}
if ([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]] && ![[self getNetworkType] isEqualToString:@"WIFI"] && ![[self getNetworkType] isEqualToString:@"NONE"]) {
dataNetworkItemView = subview;
signalStrength = [[dataNetworkItemView valueForKey:@"_signalStrengthRaw"] intValue];
break;
}
}
}
}
return signalStrength;
}