Battery Usage - IOS

2019-04-01 19:50发布

问题:

I want to show battery usage of the app on the status bar of the app. It uses GPS, so GPS signal and details of battery usage by app would be useful for the users about the battery drain.

Is there a way to get the battery usage from the device? and to show only the amount of battery drained by my app alone. On instruments it shows the graph over the time.

I want to show battery usage in numerical format, not a graph.

回答1:

Use below code to get battery level

UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batteryLevel = [myDevice batteryLevel];
_battery.text = [NSString stringWithFormat:@"%f",batteryLevel*100];
[myDevice batteryLevel];

will give you the battery between 0.0 (empty) and 1.0 (100% charged)

iOS - Issue with displaying battery status



回答2:

From command line we can get the battery details of iOS phones using libimobiledevice tools.

Inside the tools use the command:

./ideviceinfo -q com.apple.mobile.battery

It will return something like:

BatteryCurrentCapacity: 30 BatteryIsCharging: true ExternalChargeCapable: true ExternalConnected: true FullyCharged: false GasGaugeCapability: true