现在的位置: 首页 > 综合 > 正文

iOS开发- 获取精确剩余电量

2018年05月25日 ⁄ 综合 ⁄ 共 2642字 ⁄ 字号 评论关闭
  1. [UIDevice currentDevice].batteryMonitoringEnabled = YES;  
  2. double deviceLevel = [UIDevice currentDevice].batteryLevel;  

获取当前剩余电量, 我们通常采用上述方法。这也是苹果官方文档提供的。

它返回的是0.00-1.00之间的浮点值。  另外, -1.00表示模拟器。

貌似这个方法不错, 也很简单。

但是仔细观察它的返回值, 我们可以发现。 它是以0.05递变的。 折算成100% 也就是以5%来递变的。

也就是说, 这个办法是存在缺陷的, 最起码, 它不精确。

后来找到了一个方法。相当精确, 误差保持在1%以内。

我们知道, Mac下有个IOKit.framework库。 它可以计算出我们需要的电量。

如果我们要使用它的话, (iOS是不提供的) 可以先建立一个Mac下的工程, 找到IOKit.framework,那IOKit.framework里面的IOPowerSources.hIOPSKeys.h拷贝到你的iOS项目中。另外,
还需要把IOKit也导入到你的工程中去。

(当然, 如果嫌麻烦, 可以直接到我的Github中去下载工程, 导出需要的文件)

先给出我的Github下载链接:https://github.com/colin1994/batteryLevelTest.git

下面具体介绍下使用方法。

1。导入需要的IOPowerSources.h, IOPSKeys.h 和 IOKit

2。在实现的地方声明头文件

  1. #import "IOPSKeys.h"  
  2. #import "IOPowerSources.h"  


3。添加方法

  1. /** 
  2.  *  Calculating the remaining energy 
  3.  * 
  4.  *  @return Current batterylevel 
  5.  */  
  6. -(double)getCurrentBatteryLevel  
  7. {  
  8.       
  9.     //Returns a blob of Power Source information in an opaque CFTypeRef.  
  10.     CFTypeRef blob = IOPSCopyPowerSourcesInfo();  
  11.       
  12.     //Returns a CFArray of Power Source handles, each of type CFTypeRef.  
  13.     CFArrayRef sources = IOPSCopyPowerSourcesList(blob);  
  14.       
  15.     CFDictionaryRef pSource = NULL;  
  16.     const void *psValue;  
  17.       
  18.     //Returns the number of values currently in an array.  
  19.     int numOfSources = CFArrayGetCount(sources);  
  20.       
  21.     //Error in CFArrayGetCount  
  22.     if (numOfSources == 0)  
  23.     {  
  24.         NSLog(@"Error in CFArrayGetCount");  
  25.         return -1.0f;  
  26.     }  
  27.       
  28.     //Calculating the remaining energy  
  29.     for (int i = 0 ; i < numOfSources ; i++)  
  30.     {  
  31.         //Returns a CFDictionary with readable information about the specific power source.  
  32.         pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));  
  33.         if (!pSource)  
  34.         {  
  35.             NSLog(@"Error in IOPSGetPowerSourceDescription");  
  36.             return -1.0f;  
  37.         }  
  38.         psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));  
  39.           
  40.         int curCapacity = 0;  
  41.         int maxCapacity = 0;  
  42.         double percent;  
  43.           
  44.         psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));  
  45.         CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);  
  46.           
  47.         psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));  
  48.         CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);  
  49.           
  50.         percent = ((double)curCapacity/(double)maxCapacity * 100.0f);  
  51.           
  52.         return percent;  
  53.     }  
  54.     return -1.0f;  
  55. }  



4。调用方法

  1. NSLog(@"%.2f", [self getCurrentBatteryLevel]);  

抱歉!评论已关闭.