Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何准确获取iPhone电池电量?_Ios_Iphone_Batterylevel - Fatal编程技术网

Ios 如何准确获取iPhone电池电量?

Ios 如何准确获取iPhone电池电量?,ios,iphone,batterylevel,Ios,Iphone,Batterylevel,我想得到电池电量,精确到1%。 我在谷歌上找到了这个 CFTypeRef blob = IOPSCopyPowerSourcesInfo(); CFArrayRef sources = IOPSCopyPowerSourcesList(blob); CFDictionaryRef pSource = NULL; const void *psValue; int numOfSources = CFArrayGetCount(sources); if (numOfSources == 0

我想得到电池电量,精确到1%。 我在谷歌上找到了这个

    CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

CFDictionaryRef pSource = NULL;
const void *psValue;

int numOfSources = CFArrayGetCount(sources);
if (numOfSources == 0) {
    NSLog(@"Error in CFArrayGetCount");
    return -1.0f;
}

for (int i = 0 ; i < numOfSources ; i++)
{
    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;
CFTypeRef blob=IOPSCopyPowerSourcesInfo();
CFArrayRef sources=IOPSCopyPowerSourcesList(blob);
CFDictionaryRef pSource=NULL;
const void*psValue;
int numOfSources=CFArrayGetCount(源);
如果(numofources==0){
NSLog(@“CFArrayGetCount中的错误”);
返回-1.0f;
}
对于(int i=0;i
但这并不准确。 所以我在这里寻求帮助。

来自:

您可以使用UIDevice实例获取有关电池充电状态(由batteryState属性描述)和充电级别(由batteryLevel属性描述)更改的信息和通知


你知道你的循环没有意义吗?无论阵型中有多少人,你总是返回第一个命中。似乎是从这里得到的:我通过截图找到了另一种方法,并在状态栏上识别出了击球员级别号,谢谢!你的代码只精确到5,但我想精确到1,谢谢你
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
float batteryLevel = [UIDevice currentDevice].batteryLevel;

NSLog(@"battery level: %f", batteryLevel * 100);