少啰嗦, 直接贴代码, 不搞那一堆虚的!!!
设置监听
// 坑在这里, 这个一定要设置, 不然拿不到状态
// 坑在这里, 这个一定要设置, 不然拿不到状态
// 坑在这里, 这个一定要设置, 不然拿不到状态
// 开启监控
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
// 监听电池低电量模式变化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangePowerMode:) name:NSProcessInfoPowerStateDidChangeNotification object:nil];
// 监听电池状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(battery) name:UIDeviceBatteryStateDidChangeNotification object:nil];
// 电量发生变化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevels) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
状态变化
- (void)battery {
UIDeviceBatteryState batteryStatu = [UIDevice currentDevice].batteryState;
switch (batteryStatu) {
case UIDeviceBatteryStateCharging:
NSLog(@"电量: 正在充电");
break;
case UIDeviceBatteryStateFull:
NSLog(@"电量: 已充满");
break;
case UIDeviceBatteryStateUnplugged:
NSLog(@"电量: 正在放电");
break;
default:
NSLog(@"电量: 未知状态");
break;
}
}
- (void)batteryLevels {
NSLog(@"电量: 电池电量:%.02lf", [UIDevice currentDevice].batteryLevel * 100);
}
- (void)didChangePowerMode:(NSNotification *)notification {
NSLog(@"电量发生了变化");
if ([[NSProcessInfo processInfo] isLowPowerModeEnabled]) {
// low power mode on
NSLog(@"电量发生了变化: 低电量模式开");
} else {
// low power mode off
NSLog(@"电量发生了变化: 低电量模式关");
}
}
可能你们没有这个坑, 我是一直有!!! 希望有坑的可以早日填上, 其他坑也是!!!\
其他黑科技:
有缘的话希望你们都看的懂, 哈哈! 这个能获取状态栏上所有的内容, 电池电量/SIM信息/网络类型/信号强度 ... ...
// 信号强度
int signalStrength = 0;
// 网络类型
NSString *network = @"";
if (@available(iOS 13.0, *)) {
UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
id statusBar = nil;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
if ([statusBarManager respondsToSelector:@selector(createLocalStatusBar)]) {
UIView *localStatusBar = [statusBarManager performSelector:@selector(createLocalStatusBar)];
if ([localStatusBar respondsToSelector:@selector(statusBar)]) {
statusBar = [localStatusBar performSelector:@selector(statusBar)];
}
}
#pragma clang diagnostic pop
if (statusBar) {
id currentData = [[statusBar valueForKeyPath:ppwm_sstatusBar] valueForKeyPath:ppwm_currentData];
id _wifiEntry = [currentData valueForKeyPath:ppwm_wifiEntry];
id _cellularEntry = [currentData valueForKeyPath:ppwm_cellularEntry];
if (_wifiEntry && [[_wifiEntry valueForKeyPath:ppwm_isEnabled] boolValue]) {
// If wifiEntry is enabled, is WiFi.
if ([_wifiEntry isKindOfClass:NSClassFromString(ppwm_sbDataIntegerEntry)]) {
// 层级:_UIStatusBarDataNetworkEntry、_UIStatusBarDataIntegerEntry、_UIStatusBarDataEntry
signalStrength = [[_wifiEntry valueForKey:ppwm_rawValue] intValue];//displayValue
network = @"WIFI";
}
} else if (_cellularEntry && [[_cellularEntry valueForKeyPath:ppwm_isEnabled] boolValue]) {
// 获取信号强度
if ([_cellularEntry isKindOfClass:NSClassFromString(ppwm_sbDataIntegerEntry)]) {
// 层级:_UIStatusBarDataNetworkEntry、_UIStatusBarDataIntegerEntry、_UIStatusBarDataEntry
signalStrength = [[_cellularEntry valueForKey:ppwm_rawValue] intValue];//displayValue
}
// 获取网络内省
NSNumber *type = [_cellularEntry valueForKeyPath:@"type"];
if (type) {
switch (type.integerValue) {
case 0:
//无sim卡
network = @"NONE";
break;
case 1:
network = @"1G";
break;
case 4:
network = @"3G";
break;
case 5:
network = @"4G";
break;
default:
//默认WWAN类型
network = @"WWAN";
break;
}
}
}
}
} else {
if ([PPWMCommonUtil ppwm_isNotchScreen]) {
// 刘海屏
id statusBar = [[UIApplication sharedApplication] valueForKeyPath:ppwm_statusBar];
id statusBarView = [statusBar valueForKeyPath:ppwm_statusBar];
UIView *foregroundView = [statusBarView valueForKeyPath:ppwm_foregroundView];
NSArray *subviews = [[foregroundView subviews][2] subviews];
if (subviews.count == 0) {
// iOS 12
id currentData = [statusBarView valueForKeyPath:ppwm_currentData];
id wifiEntry = [currentData valueForKey:ppwm_wifiEntry];
if ([[wifiEntry valueForKey:ppwm_enabled] boolValue]) {
network = @"WIFI";
}else {
// 卡1:
id cellularEntry = [currentData valueForKey:ppwm_cellularEntry];
// 卡2:
id secondaryCellularEntry = [currentData valueForKey:ppwm_sCellularEntry];
if (([[cellularEntry valueForKey:ppwm_enabled] boolValue]|[[secondaryCellularEntry valueForKey:ppwm_enabled] boolValue]) == NO) {
// 无卡情况
network = @"NONE";
}else {
// 判断卡1还是卡2
BOOL isCardOne = [[cellularEntry valueForKey:ppwm_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(ppwm_sbWifiSignalView)]) {
signalStrength = [[subview valueForKey:ppwm_numberOfActiveBars] intValue];
network = @"WIFI";
break;
}else if ([subview isKindOfClass:NSClassFromString(ppwm_sbStringView)]) {
signalStrength = [[subview valueForKey:ppwm_numberOfActiveBars] intValue];
network = [subview valueForKeyPath:ppwm_originalText];
break;
}
}
}
} else {
// 非刘海屏
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:ppwm_statusBar] valueForKey:ppwm_foregroundView] subviews];
NSString *dataNetworkItemView = nil;
int networkType = -1;
for (id subview in subviews) {
if ([subview isKindOfClass:NSClassFromString(ppwm_sbDataNetworkItemView)]) {
networkType = [[subview valueForKeyPath:ppwm_dataNetworkType] intValue];
break;
}
}
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;
}
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(ppwm_sbDataNetworkItemView) class]] && networkType == 5) {
dataNetworkItemView = subview;
signalStrength = [[dataNetworkItemView valueForKey:ppwm_wifiStrengthRaw] intValue];
// signalStrength = [[dataNetworkItemView valueForKey:@"_wifiStrengthBars"] intValue];
break;
}
if ([subview isKindOfClass:[NSClassFromString(ppwm_sbSignalStrengthItemView) class]] && networkType != 5 && networkType !=0) {
dataNetworkItemView = subview;
signalStrength = [[dataNetworkItemView valueForKey:ppwm_signalStrengthRaw] intValue];
// signalStrength = [[dataNetworkItemView valueForKey:@"_signalStrengthBars"] intValue];
break;
}
}
}
}
if ([network isEqualToString:@""]) {
network = @"NO DISPLAY";
}
尽情去拿吧!!!