Battery Usage - IOS

断了今生、忘了曾经 提交于 2019-12-05 05:21:42

问题


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



来源:https://stackoverflow.com/questions/16396928/battery-usage-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!