Manjusaka

Manjusaka

How to detect if an iPhone is in low power mode

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 do we know 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.

One important thing to note is 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

Low Power Mode is automatically turned off when your phone is charged to 80% or above.

Detecting Low Power Mode#

It turns out that getting information about Low Power Mode in iOS 9 is quite easy. 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 functionality 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 adding this method, it will monitor the power mode and respond when it switches.

    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
      }
    }

Tip:

  • 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.