We can use the batteryLevel property of the UIDevice class for this. It will return a float:
1 |
float myFloat = [[UIDevice currentDevice] batteryLevel]; |
If you’d like to print this in a UILabel you’ll have to convert it into a String Object like so:
1 |
self.myLabel.text = [NSString stringWithFormat:@"%f", myFloat]; |
For this to work battery monitoring needs to be enabled, perhaps in the viewDidLoad method. Otherwise the returned value will be -1.0:
1 |
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; |