ios UIDeviceBattery【电池的状态处理:电池状态获取及监测、电池电量获取及监测、低电量模式切换监测】

Kanya ·
更新时间:2024-09-21
· 849 次阅读

前言 需求:电量低于 <= 50 %进行报警

name:NSProcessInfoPowerStateDidChangeNotification

UIDeviceBatteryLevelDidChangeNotification

[[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceBatteryLevelDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) { // Level has changed [UIDevice currentDevice].batteryMonitoringEnabled = YES; NSLog(@"Battery Level Change"); NSLog(@"电池电量:%.2f", [UIDevice currentDevice].batteryLevel); }];

UIDeviceBatteryStateDidChangeNotification

一、电池状态获取及监测

code

#pragma mark - 电池状态获取及监控 -(void)checkAndMonitorBatteryState{ UIDevice * device = [UIDevice currentDevice]; //是否允许监测电池 //要想获取电池状态和监控电池状态 必须允许 device.ba 了解本专栏 订阅专栏 解锁全文
作者:iOS进阶



电池 IOS

需要 登录 后方可回复, 如果你还没有账号请 注册新账号