Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's usually considered best practice to avoid polling and instead ask for notifications from the system, like so:</p> <pre><code>[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelUpdate) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; </code></pre> <p>…where the <code>batteryLevelUpdate</code> would look like this:</p> <pre><code>- (void)batteryLevelUpdate:(NSNotification *)notification { // do whatever, using: [[UIDevice currentDevice] batteryLevel] } </code></pre> <p>From the <a href="http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/uid/TP40006902-CH3-SW20">UIDevice Class Reference</a>:</p> <blockquote> <p>Notifications for battery level change are sent no more frequently than once per minute. Do not attempt to calculate battery drainage rate or battery time remaining; drainage rate can change frequently depending on built-in applications as well as your application.</p> </blockquote> <p>Once per minute is 10x more often than your code is currently checking while exerting much less effort CPU-wise. It does <em>not</em> mention, however what granularity of changes will cause notifications -- is a 0.01% change going to send, or does it require >1% change?</p> <p>To answer your other question if you should set the <code>setBatteryMonitoringEnabled</code> back to <code>NO</code> : if you are using the notifications and not manually polling for batteryStatus then the answer is that you must leave it at <code>YES</code>, or risk missing the notifications.</p> <p><a href="http://developer.apple.com/library/ios/#samplecode/BatteryStatus/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008812-Intro-DontLinkElementID_2">Apple's official BatteryStatus Sample Code</a> uses this same take on a battery-status reporting.</p> <p>There is also a <code>UIDeviceBatteryStateDidChangeNotification</code> which will notify you when a device is discharging (in use), charging or has become fully charged.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload