I'm using this function to get current battery level of device:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
double batLeft = (float)[myDevice batteryLevel];
NSLog(@"%f",batLeft);
but the result has a 5% granularity. Example: when the phone battery is at 88%, it only logs a value of 0.85. batteryLevel
only returns values in increments of 0.05. For example: 0.85, 0.9, 0.95 and never returns values like 0.82 or 0.83.
Is there any solution to get a percentage with a higher precision?
Connect to WiFi and charge your battery Sometimes it's the little things that will block an iOS update. You should have a strong, reliable WiFi connection and your iPhone needs to have at least 50 percent of battery life remaining.
See the iPhone battery percentage in the status bar On an iPhone with Face ID: Swipe down from the top-right corner. On an iPhone with a Home button: Go to Settings > Battery, then turn on Battery Percentage.
Xcode 11 • Swift 5.1 Then you can create a computed property to return the battery level: Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled. If battery monitoring is not enabled, battery state is UIDevice.
There are at least four different ways to read the battery level, and all four ways may return different values.
Here is a chart of these values through time.
The values were recorded with this iOS project: https://github.com/nst/BatteryChart
Please check out the code for reference.
check out this site : Reading the battery level programmatically
but, carefully use. all of the APIs used here are undocumented on the iPhone, and will probably lead to a rejection if you submit this application to the App Store. Although battery charge status is not exactly, I'd recommend using the UIDevice battery monitoring methods.
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
double batLeft = (float)[myDevice batteryLevel] * 100;
NSLog(@"%.f", batLeft);
NSString * levelLabel = [NSString stringWithFormat:@"%.f%%", batLeft];
lblLevel.text = levelLabel;
Swift version to get the battery level:
UIDevice.current.isBatteryMonitoringEnabled = true
let batteryLevel = UIDevice.current.batteryLevel
batteryLevel
return 0,39; 0,40 values for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With