title: Getting iPhone's battery level
body: <p>I have a simple question. How do I get iPhone's battery level?</p>
  
  <pre><code>[UIDevice currentDevice] batteryLevel]
  </code></pre>
  
  <p>Simple enough? However there is a <em>little catch</em> - <strong><em>I can't use UIKit</em></strong>. Here is what I wrote so far, but I don't think it works:</p>
  
  <pre><code>    // 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, &amp;powerIterator);
  
  NSLog(@"Kernel said %d",kr);
  
  power((void*)nport,powerIterator);
  </code></pre>
  
  <p>I'm still pretty sure you have to rely on <strong>IOKit</strong> 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 :</p>
  
  <p><a href="http://img837.imageshack.us/img837/1829/screenshot20100718at211.png" rel="nofollow noreferrer">alt text http://img837.imageshack.us/img837/1829/screenshot20100718at211.png</a></p>