Detecting low power mode

I read a story this week about the Uber App knowing when your phone is in power saving mode. Uber found people more likely to pay higher rates when their phone is about to die. The company claims they do not use the data to set prices but it got me wondering how can you detect low power mode with iOS?

Low Power Mode

With iOS 9 Apple added a Low Power Mode to the iPhone. It extends battery life by stopping some battery heavy features such as email fetch, Hey Siri and background app refresh until you can recharge the device.

It is important to understand that it is the user who decides to enter low power mode. You need to go into the battery settings to turn it on. When you do the battery in the status bar turns yellow:

Low Power Mode

It turns off automatically once you charge the device to 80%.

Detecting Low Power mode

It turns out that it is easy to test for low power mode in iOS 9. You can check if the user has switched to low power mode from the NSProcessInfo class:

// Swift
if NSProcessInfo.processInfo().lowPowerModeEnabled {
  // stop battery intensive actions
}

If you are doing this in Objective-C:

// Objective-C
if ([[NSProcessInfo processInfo] isLowPowerModeEnabled]) {
  // stop battery intensive actions
}

To respond when the user changes the mode you need to listen for the NSProcessInfoPowerStateDidChangeNotification notification. For example, in the viewDidLoad method of a view controller:

// Swift
NSNotificationCenter.defaultCenter().addObserver(self,
  selector: #selector(didChangePowerMode(_:)),
  name: NSProcessInfoPowerStateDidChangeNotification,
  object: nil)

// Objective-C
[[NSNotificationCenter defaultCenter] addObserver:self
  selector:@selector(didChangePowerMode:)
  name:NSProcessInfoPowerStateDidChangeNotification
  object:nil];

As somebody reminded me after I first posted this it is not necessary to remove the observer when the view controller goes away as long as we are only targetting iOS 9.

Then in the action check the power mode and respond:

// Swift
func didChangePowerMode(notification: NSNotification) {
    if NSProcessInfo.processInfo().lowPowerModeEnabled {
      // low power mode on
    } else {
      // low power mode off
    }
}

// Objective-C
- (void)didChangePowerMode:(NSNotification *)notification {
  if ([[NSProcessInfo processInfo] isLowPowerModeEnabled]) {
    // low power mode on
  } else {
    // low power mode off
  }
}

Notes:

Detecting that the user has turned low power mode on is only useful if your app is able to take some energy-saving measures to prolong battery life. Some suggestions from Apple:

Further Reading

Never miss a post!

iOS Size Classes Cheat Sheet

Subscribe and get my free iOS Size Classes Cheat Sheet

Unsubscribe at any time.
No time to watch WWDC videos?

Sign up to get my iOS posts direct to your inbox and I will send you a free PDF of my iOS Size Classes Cheat Sheet.

Unsubscribe at any time.
Archives Categories
comments powered by Disqus