I have a simple question. How do I get iPhone's battery level?
[UIDevice currentDevice] batteryLevel]
Simple enough? However there is a little catch - I can't use UIKit. Here is what I wrote so far, but I don't think it works:
// notification port
IONotificationPortRef nport = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(nport), kCFRunLoopDefaultMode);
CFMutableDictionaryRef matching = IOServiceMatching("IOPMPowerSource");
kern_return_t kr = IOServiceAddMatchingNotification(nport, kIOFirstMatchNotification, matching, (IOServiceMatchingCallback)power, self, &powerIterator);
NSLog(@"Kernel said %d",kr);
power((void*)nport,powerIterator);
I'm still pretty sure you have to rely on IOKit in order to retrieve battery level. My application does not use UIKit as it is a low-level application in which UIKit cannot be used. Here are the frameworks I am using :