美文网首页v2panda的技术专题iOS技能iOS奋斗
iOS开发之runtime精准获取电池电量

iOS开发之runtime精准获取电池电量

作者: 操哥 | 来源:发表于2015-11-15 22:34 被阅读10088次

方法一:通过苹果官方文档里面UIDevice public API来获取,代码如下:

  [UIDevice currentDevice].batteryMonitoringEnabled = YES;
  [[NSNotificationCenter defaultCenter]
 addObserverForName:UIDeviceBatteryLevelDidChangeNotification
 object:nil queue:[NSOperationQueue mainQueue]
 usingBlock:^(NSNotification *notification) {
     // Level has changed
     NSLog(@"Battery Level Change");
     NSLog(@"电池电量:%.2f", [UIDevice currentDevice].batteryLevel);
 }];
@property(nonatomic,readonly) float     batteryLevel NS_AVAILABLE_IOS(3_0);  
// 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown它返回的是0.00-1.00之间的浮点值。  

但是经过测试发现,在iOS7 上 它是以0.05为单位的,但是在iOS9下测试,它是以0.01为单位的,虽然也是0.01为单位,但是测试多次也会出现偏差1%左右。也就是说, 这个办法是存在缺陷的, 最起码, 它不精确。

方法二:找到Mac下IOKit.framework,将IOKit.framework里面的IOPowerSources.h和IOPSKeys.h拷贝到你的iOS项目中。另外, 还需要把IOKit也导入到你的工程中去,此方法也会出现偏差,不精确。DEMO 地址:https://github.com/colin1994/batteryLevelTest.git

/** 
*  Calculating the remaining energy 
* 
*  @return Current batterylevel 
*/  
-(double)getCurrentBatteryLevel  
{  
  
//Returns a blob of Power Source information in an opaque CFTypeRef.  
CFTypeRef blob = IOPSCopyPowerSourcesInfo();  
  
//Returns a CFArray of Power Source handles, each of type CFTypeRef.  
CFArrayRef sources = IOPSCopyPowerSourcesList(blob);  
  
CFDictionaryRef pSource = NULL;  
const void *psValue;  
  
//Returns the number of values currently in an array.  
int numOfSources = CFArrayGetCount(sources);  
  
//Error in CFArrayGetCount  
if (numOfSources == 0)  
{  
    NSLog(@"Error in CFArrayGetCount");  
    return -1.0f;  
}  
  
//Calculating the remaining energy  
for (int i = 0 ; i < numOfSources ; i++)  
{  
    //Returns a CFDictionary with readable information about the specific power source.  
    pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));  
    if (!pSource)  
    {  
        NSLog(@"Error in IOPSGetPowerSourceDescription");  
        return -1.0f;  
    }  
    psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));  
      
    int curCapacity = 0;  
    int maxCapacity = 0;  
    double percent;  
      
    psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));  
    CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);  
      
    psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));  
    CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);  
      
    percent = ((double)curCapacity/(double)maxCapacity * 100.0f);  
      
    return percent;  
}  
return -1.0f;  
}  

方法三:通过runtime 获取StatusBar上电池电量控件类私有变量的值,此方法可精准获取iOS6以上电池电量

MRC:
- (int)getCurrentBatteryLevel
{
 if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive||[UIApplication sharedApplication].applicationState==UIApplicationStateInactive) {
        void *result = nil;
        object_getInstanceVariable([UIApplication sharedApplication], "_statusBar", &result);
        id status  = result;
        for (id aview in [status subviews]) {
            for (id bview in [aview subviews]) {
                int batteryLevel = 0;
               if ([NSStringFromClass([bview class]) caseInsensitiveCompare:@"UIStatusBarBatteryItemView"] == NSOrderedSame&&[[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
                {
                    object_getInstanceVariable(bview, "_capacity", &result);
                    batteryLevel = (int)result;
                    NSLog(@"电池电量:%d",batteryLevel);
                    if (batteryLevel > 0 && batteryLevel <= 100) {
                        return batteryLevel;
                        
                    } else {
                        return 0;
                    }
                }

        }
    }
    
    return 0;
}

ARC:
- (int)getCurrentBatteryLevel
{

UIApplication *app = [UIApplication sharedApplication];
if (app.applicationState == UIApplicationStateActive||app.applicationState==UIApplicationStateInactive) {
    Ivar ivar=  class_getInstanceVariable([app class],"_statusBar");
    id status  = object_getIvar(app, ivar);
    for (id aview in [status subviews]) {
        int batteryLevel = 0;
        for (id bview in [aview subviews]) {
            if ([NSStringFromClass([bview class]) caseInsensitiveCompare:@"UIStatusBarBatteryItemView"] == NSOrderedSame&&[[[UIDevice currentDevice] systemVersion] floatValue] >=6.0)
            {
            
                    Ivar ivar=  class_getInstanceVariable([bview class],"_capacity");
                    if(ivar)
                    {
                        batteryLevel = ((int (*)(id, Ivar))object_getIvar)(bview, ivar);
                        //这种方式也可以
                        /*ptrdiff_t offset = ivar_getOffset(ivar);
                         unsigned char *stuffBytes = (unsigned char *)(__bridge void *)bview;
                         batteryLevel = * ((int *)(stuffBytes + offset));*/
                        NSLog(@"电池电量:%d",batteryLevel);
                        if (batteryLevel > 0 && batteryLevel <= 100) {
                            return batteryLevel;
                            
                        } else {
                            return 0;
                        }
                    }
                
            }
            
        }
    }
}

return 0;
}

相关文章

网友评论

  • 乐视薯片:楼主,你好,我用runtime获取电量,也是0.05为单位的,不是很精准啊
  • 花果山松鼠:楼主,我试了第三种方法,每次运行得到的数据都是100,是不是不对啊。
    Nick_Adams:解决了吗?我的也是100
  • 4a23294480e9:代码的框怎么弄的~
    ArchLL:@NicoJohn markdown
  • cb4f84403640:如果在设置里面没有开启电量的百分比显示 获取到的是0
    操哥:@我是叉叉歪 已解决
    操哥:@我是叉叉歪 😓好吧,如何破?
  • 4b4d42e9a1bb:这个是还需要导入一个库文件吗,怎么找不到object_getlnstanceVariable方法呢
    4b4d42e9a1bb:@我是叉叉歪 谢谢
    cb4f84403640:@吴章平 #import <objc/runtime.h>
    操哥:@吴章平 已更新
  • 小赢一场:很好,上次我们用了私有的api也没被拒,哈哈
    大亮Coder:@a79860bfcaf5 :+1:...不懂苹果的审核机制
    小赢一场:@大亮Coder 没有
    大亮Coder:@a79860bfcaf5 有做加密处理吗?
  • scyworld:同楼上
  • 大亮Coder:但是用了私有变量,上架审核会通不过的吧?
    张月半:@大亮Coder 好的谢谢
    大亮Coder:@一条浪味仙 苹果明文规定不准使用私有API,但是有人是用了也没被苹果审核出来,被审核出来肯定被拒。有些人还说,用字符串加密的办法能逃过审核
    张月半:@大亮Coder 请问有改变私有变量的操作就会被拒吗 比如改变textfield删除按钮的样式

本文标题:iOS开发之runtime精准获取电池电量

本文链接:https://www.haomeiwen.com/subject/ngykhttx.html