//
// NetworkInformation.m
// Network
//
//
#import "NetworkInformation.h"
#import "AppDelegate.h"
#import
#import
#import
@implementation NetworkInformation
//刘海屏safeAreaInset的高度(所有的刘海屏都一致)
static const CGFloat liuHaiHeight = 44;
#pragma mark获取当前网络类型
#pragma mark获取Wifi信号强度
+ (int)getWifiSignalStrength
{
intsignalStrength =0;
// 判断是否为iOS 13
if(@available(iOS13.0, *)) {
UIStatusBarManager*statusBarManager = [UIApplicationsharedApplication].keyWindow.windowScene.statusBarManager;
idstatusBar =nil;
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wundeclared-selector"
if([statusBarManagerrespondsToSelector:@selector(createLocalStatusBar)]) {
UIView*localStatusBar = [statusBarManagerperformSelector:@selector(createLocalStatusBar)];
if([localStatusBarrespondsToSelector:@selector(statusBar)]) {
statusBar = [localStatusBarperformSelector:@selector(statusBar)];
}
}
#pragma clang diagnostic pop
if(statusBar) {
idcurrentData = [[statusBarvalueForKeyPath:@"_statusBar"]valueForKeyPath:@"currentData"];
signalStrength = [selfgetCurrentEntry:currentData];
}
}else{
UIApplication *app = [UIApplication sharedApplication];
idstatusBar = [appvalueForKey:@"statusBar"];
if([[[selfalloc]init]isLiuHaiScreen]) {
// 刘海屏
idstatusBarView = [statusBarvalueForKeyPath:@"statusBar"];
UIView*foregroundView = [statusBarViewvalueForKeyPath:@"foregroundView"];
NSArray*subviews = [[foregroundViewsubviews][2]subviews];
if(subviews.count==0) {
// iOS 12
idcurrentData = [statusBarViewvalueForKeyPath:@"currentData"];
idwifiEntry = [currentDatavalueForKey:@"wifiEntry"];
signalStrength = [[wifiEntryvalueForKey:@"displayValue"]intValue];
// dBm
// int rawValue = [[wifiEntry valueForKey:@"rawValue"] intValue];
}else{
for(idsubviewinsubviews) {
if([subviewisKindOfClass:NSClassFromString(@"_UIStatusBarWifiSignalView")]) {
signalStrength = [[subviewvalueForKey:@"_numberOfActiveBars"]intValue];
}
}
}
}else{
// 非刘海屏
UIView*foregroundView = [statusBarvalueForKey:@"foregroundView"];
NSArray*subviews = [foregroundViewsubviews];
NSString*dataNetworkItemView =nil;
for(idsubviewinsubviews) {
if([subviewisKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView")class]]) {
dataNetworkItemView = subview;
break;
}
}
signalStrength = [[dataNetworkItemViewvalueForKey:@"_wifiStrengthBars"]intValue];
returnsignalStrength;
}
}
returnsignalStrength;
}
#pragma mark判断是否是刘海屏
+ (BOOL)isLiuHaiScreen
{
if(@available(iOS11.0, *)) {
UIEdgeInsetssafeAreaInsets = [UIApplicationsharedApplication].windows[0].safeAreaInsets;
returnsafeAreaInsets.top==liuHaiHeight|| safeAreaInsets.bottom==liuHaiHeight|| safeAreaInsets.left==liuHaiHeight|| safeAreaInsets.right==liuHaiHeight;
}else{
returnNO;
}
}
+(int)getCurrentEntry:(id)currentData{
intsignalStrength =0;
intsignalStrength1 =0;
idwifiEntry = [currentDatavalueForKeyPath:@"wifiEntry"];
if ([wifiEntry isKindOfClass:NSClassFromString(@"_UIStatusBarDataIntegerEntry")]) {
signalStrength = [[wifiEntryvalueForKey:@"displayValue"]intValue];
}
idcellularEntry = [currentDatavalueForKeyPath:@"_cellularEntry"];
if ([cellularEntry isKindOfClass:NSClassFromString(@"_UIStatusBarDataIntegerEntry")]) {
signalStrength1 = [[cellularEntryvalueForKey:@"_displayValue"]intValue];
}
NSLog(@"cellularEntry -- %ld",(long)signalStrength1);
returnsignalStrength;
}
@end