Manjusaka

Manjusaka

How to detect if an iPhone is in low power mode.

title: How to Detect Low Power Mode on iPhone
type: tags
date: 2016-07-23 03:09:38
tags: [iOS, programming, Objective-C]
categories: [programming, translation]
toc: true#

This week, I read an article about how Uber detects when a phone is in low power mode. (Note: The article link is Uber found people more likely to pay) When people's phones are about to shut down, using Uber may result in higher prices. The company (Uber) claims that they do not use the data of whether the phone is in low power mode for pricing, but here I want to know how we can detect if a user's iPhone is in low power mode.

Low Power Mode#

In iOS 9, Apple added a new feature called Low Power Mode for iPhones. Low Power Mode extends your battery life by disabling battery-consuming features such as mail fetch, Siri, and background app refresh until you can charge your phone.

It is important to note that entering Low Power Mode is a decision made by the user. You need to go into the battery settings to enable Low Power Mode. When you enter Low Power Mode, the battery icon in the status bar turns yellow.

Low Power Mode

When you charge your phone to over 80%, Low Power Mode will automatically turn off.

Detecting Low Power Mode#

It turns out that in iOS 9, it is very easy to obtain information about Low Power Mode. You can use the NSProcessInfo class to determine if the user has entered Low Power Mode:

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

If you want to implement this feature in Objective-C:

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

If you listen for the NSProcessInfoPowerStateDidChangeNotification notification, you will receive a message when the user switches to Low Power Mode. For example, in the viewDidLoad method of a view controller:

    NSNotificationCenter.defaultCenter().addObserver(self,
      selector: #selector(didChangePowerMode(_:)),
      name: NSProcessInfoPowerStateDidChangeNotification,
      object: nil)
    [[NSNotificationCenter defaultCenter] addObserver:self
      selector:@selector(didChangePowerMode:)
      name:NSProcessInfoPowerStateDidChangeNotification
      object:nil];

After I published this article for the first time, many people reminded me that for developers who only target iOS 9.X, it is not necessary to remove the observer when the ViewController disappears.

Then, in this method, you can monitor the power mode and respond to the switch:

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

Tips:

  • This notification method and the property in NSProcessInfo are new methods provided in iOS 9. If you want your app to be compatible with iOS 8 or earlier versions, you need to test your code for availability on this website: test for availability.

  • Low Power Mode is a feature exclusive to iPhones. If you test the above code on an iPad, it will always return false.

Detecting if a user has enabled Low Power Mode is only useful if your app can take some energy-saving measures to extend battery life. Apple provides some suggestions:

  • Stop updating location
  • Reduce user interaction animations
  • Disable background operations such as data streaming
  • Disable special effects
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.