Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Last active August 26, 2023 18:26
Show Gist options
  • Save juliengdt/cfad1bbb619bcad6bfc9ecb28fb5149f to your computer and use it in GitHub Desktop.
Save juliengdt/cfad1bbb619bcad6bfc9ecb28fb5149f to your computer and use it in GitHub Desktop.
Detect and make your iPhone app responds to low-power mode

Low Power Mode - LPM

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.

Detection

To detect LPM you have to be in the couple iOS 9.X / iPhone:

  • For iOS 8.X, you need test availablity before use it
  • LPM is a iPhone-only feature. If you call it on an iPad, it will return you always false

What to do

Reading Apple doc, there are some effects you might do on LPM, such as:

  • Stop location updates
  • Limit the use of animations
  • Stop background activities such as networking
  • Disable motion effects

More Reading

Apple Guidelines

Your Obligation as a Developer

Even small inefficiencies in apps add up, significantly affecting battery life, performance, and responsiveness. As an app developer, you have an obligation to make sure your app runs as efficiently as possible. Use recommended APIs so the system can make smart decisions about how best to manage your app and the resources it uses. Whenever possible, batch and reduce network operations, and avoid unnecessary updates to the user interface. Power-intensive operations should be under the user’s control. If a user is playing a graphics-heavy game, for example, the user should not be surprised if the activity consumes power. Strive to make your app absolutely idle when it is not responding to user input.

By adhering to recommended guidelines, you can make big contributions to the overall energy efficiency of the platform and the satisfaction of your users.

// One-Shot Detection
if NSProcessInfo.processInfo().lowPowerModeEnabled {
// stop battery intensive actions
}
// Better Detection, ie in the viewDidLoad of your viewController or in the AppDelegate
// Observer
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(didChangePowerMode(_:)),
name: NSProcessInfoPowerStateDidChangeNotification,
object: nil)
// Associated function
func didChangePowerMode(notification: NSNotification) {
if NSProcessInfo.processInfo().lowPowerModeEnabled {
// low power mode on
} else {
// low power mode off
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment