Get battery information for your iOS device: Battery level

Source: Internet
Author: User

This article describes how to get the battery information for an iOS device through the API.
The power consumption of mobile devices has always been a big problem, and it is inevitable that app development will need to collect information about the power consumption of the app when it runs, which is one of the metrics for app performance.
First you need to turn on battery statistics in iphone settings.

1. Get through instruments

The Energy diagnostics tool comes with the instruments tool to get power consumption information for specific hours of the iphone. Specific steps:
Open the developer option in the start logging-> disconnect iphone with PC connection, a series of user actions, Stop logging-> connect iphone and PC, import power consumption data into instrument S
But the information obtained in this way is not very intuitive.

2. Get through Uidevice

Uidevice provides detailed information about the current iOS device, such as name, Systemversion, Localizedmodel, Batterylevel, and more.

UIDevice.currentDevice.batteryMonitoringEnabled = truelet batteryLevel = UIDevice.currentDevice().batteryLevelUIDevice.currentDevice.batteryMonitoringEnabled = false

The power of the iOS system can be obtained through uidevice, but before iOS 8.0, the batterylevel in Uidevice can only be accurate to 5%, requiring other means of obtaining 1% accuracy of battery information. After iOS 8.0, 1% accuracy starts to be supported.

3. Get through the Iokit framework

The IOKIT framework communicates with hardware or kernel services in iOS and is often used to obtain hardware details.
First, you need to import iopowersources.h,iopskeys.h,iokit three files into your project. You can then get 1% accurate battery information with the following code:

-(Double) getbatterylevel{//Returns a BLOB of power source information in an opaque cftyperefCftyperef blob = Iopscopypowersourcesinfo ();//Returns a cfarray of power source handles, each of the type CftyperefCfarrayref sources = iopscopypowersourceslist (BLOB); Cfdictionaryref Psource =NULL;Const void*psvalue;//Returns the number of values currently in an array    intNumofsources = Cfarraygetcount (sources);//Error in Cfarraygetcount    if(Numofsources = =0) {NSLog(@"Error in Cfarraygetcount");return-1.0F }//Calculating the remaining energy     for(intI=0; i<numofsources; i++) {//Returns a cfdictionary with readable information about the specific power sourcePsource = Iopsgetpowersourcedescription (blob, cfarraygetvalueatindex (sources, i));if(!psource) {NSLog(@"Error in Iopsgetpowersourcedescription");return-1.0F } Psvalue = (cfstringref) cfdictionarygetvalue (Psource, Cfstr (Kiopsnamekey));intCurcapacity =0;intMaxcapacity =0;DoublePercentage        Psvalue = Cfdictionarygetvalue (Psource, Cfstr (Kiopscurrentcapacitykey));        Cfnumbergetvalue ((cfnumberref) Psvalue, Kcfnumbersint32type, &curcapacity);        Psvalue = Cfdictionarygetvalue (Psource, Cfstr (Kiopsmaxcapacitykey));        Cfnumbergetvalue ((cfnumberref) Psvalue, Kcfnumbersint32type, &maxcapacity); Percentage = ((Double) Curcapacity/(Double) maxcapacity *100.0f);NSLog(@"curcapacity:%d/maxcapacity:%d, percentage:%.1f", curcapacity, maxcapacity, percentage);returnPercentage }return-1.0F;}

Of course, the premise is still to set batterymonitoringenabled to true.
Finally, you can view and analyze the power consumption of the app.

Get battery information for your iOS device: Battery level

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.