20 #if defined(__APPLE__) && defined(__MACH__) && defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
21 #define __IPHONEOS__ (__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__*1000)
24 #if defined(__IPHONEOS__)
26 #import <UIKit/UIDevice.h>
30 #import <Foundation/Foundation.h>
31 #import <IOKit/ps/IOPowerSources.h>
32 #import <IOKit/ps/IOPSKeys.h>
33 #import <IOKit/pwr_mgt/IOPM.h>
38 namespace battery_info {
42 #if defined(__IPHONEOS__)
43 UIDevice.currentDevice.batteryMonitoringEnabled = YES;
44 if (UIDevice.currentDevice.batteryState == UIDeviceBatteryStateUnknown) {
58 #if !defined(__IPHONEOS__)
59 inline NSDictionary* get_iops_battery_info() {
61 CFTypeRef
info = IOPSCopyPowerSourcesInfo();
67 CFArrayRef list = IOPSCopyPowerSourcesList(
info);
69 if (list == NULL || !CFArrayGetCount(list)) {
77 CFDictionaryRef battery = CFDictionaryCreateCopy(NULL, IOPSGetPowerSourceDescription(
info, CFArrayGetValueAtIndex(list, 0)));
82 return static_cast<NSDictionary*
>(battery);
87 #if defined(__IPHONEOS__)
88 return UIDevice.currentDevice.batteryLevel * 100;
91 NSDictionary* battery = get_iops_battery_info();
93 NSNumber *maxCapacity = [battery objectForKey:@kIOPSMaxCapacityKey];
94 NSNumber *currentCapacity = [battery objectForKey:@kIOPSCurrentCapacityKey];
96 if (maxCapacity && currentCapacity) {
97 return ([currentCapacity doubleValue] / [maxCapacity doubleValue]) * 100;
double get_battery_percentage()
bool does_device_have_battery()