gpt4 book ai didi

ios - 电池状态总是返回 Null

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:51 24 4
gpt4 key购买 nike

我有下面的方法可以正确返回电池电量,但无法获取电池状态。它总是返回 UNKNOWN。

-(NSString *)batteryStatus
{
UIDevice *device = [UIDevice currentDevice];
NSString *batteryStateString = nil;
switch(device.batteryState)
{
case UIDeviceBatteryStateUnplugged: batteryStateString = @"Unplugged"; break;
case UIDeviceBatteryStateCharging: batteryStateString = @"Charging"; break;
case UIDeviceBatteryStateFull: batteryStateString = @"Full"; break;
default: batteryStateString = @"Unknown"; break;
}

[device setBatteryMonitoringEnabled:YES];
NSString *statusString = [NSString stringWithFormat:@"Level - %d%% - State - %@",
(int)round(device.batteryLevel * 100), batteryStateString];
[device setBatteryMonitoringEnabled:NO];
return statusString;
}

如何从 UIDevice 返回正确的电池状态?

最佳答案

尝试在切换前移动这条线。

[device setBatteryMonitoringEnabled:YES];

默认禁用电池监控会阻止您正确读取电池状态。

关于ios - 电池状态总是返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17329030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com