title: Return Capacity Values, Not Percent
body: <p>Using this method here how would I return the curCapacity and maxCapacity mAh values rather than a percent?</p>
  
  <p><a href="http://blog.coriolis.ch/2009/02/14/reading-the-battery-level-programmatically/comment-page-1/#comment-6089" rel="nofollow noreferrer">http://blog.coriolis.ch/2009/02/14/reading-the-battery-level-programmatically/comment-page-1/#comment-6089</a></p>
  
  <p>No matter what I try my curCapacity and maxCapacity values match my battery percentage!</p>
  
  <p>My 1st attempt</p>
  
  <pre><code>#include "IOPowerSources.h"
  #include "IOPSKeys.h"
  
  - (double) batteryLevel // Find current charge percentage
  {
  #if TARGET_IPHONE_SIMULATOR
      return 80.0f;
  #else
      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 &lt; 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, &amp;curCapacity);
  
          psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
          CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &amp;maxCapacity);
  
          percent = ((double)curCapacity/(double)maxCapacity * 100.0f);
  
          return percent;
      }
      return -1.0f;
  #endif
  }
  
  
  #include "IOPowerSources.h"
  #include "IOPSKeys.h"
  
  - (double) curCapacity // Find current capacity
  {
  #if TARGET_IPHONE_SIMULATOR
      return 460;
      #else
      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 &lt; 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 curCapacityVal;
  
          psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
          CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &amp;curCapacity);
  
          psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
          CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &amp;maxCapacity);
  
          curCapacityVal = (double)curCapacity;
  
          return curCapacityVal;
      }
      return -1.0f;
  #endif
  }
  
  
  
  
  #include "IOPowerSources.h"
  #include "IOPSKeys.h"
  
  - (double) maxCapacity // Find maximum capacity
  {
  #if TARGET_IPHONE_SIMULATOR
      return 780;
  #else
      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 &lt; 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 maxCapacityVal;
  
          psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
          CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &amp;curCapacity);
  
          psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
          CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &amp;maxCapacity);
  
          maxCapacityVal = (double)maxCapacity;
  
          return maxCapacityVal;
  
      }
      return -1.0f;
  #endif
  }
  </code></pre>