how to detect low power mode in swift

How to detect low power mode in swift

What is low power mode?
Low power mode is a new way that apple is tackling their ever dwindling batteries. It stops all background activity, dims the screen and uses different assets to save power. It’s pretty neat to be fair, I have survived on 1% for half an hour before. I know, Sounds like a lie, but trust me. It isn’t.

The first thing we need to do is declare some variables at the top. So, in your ViewController.Swift file. just under the class declaration, write:

 

func doComplicatedWork() {
guard ProcessInfo.processInfo.isLowPowerModeEnabled == false else { return }

// continue doing complicated work here
}

When that method is triggered, you can check the new value of isLowPowerModeEnabled to see what state the device is in:

@objc func powerStateChanged(_ notification: Notification) {
let lowerPowerEnabled = ProcessInfo.processInfo.isLowPowerModeEnabled
// take appropriate action
}

Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top