Show Menu

Looking for an iOS Developer?

Submit your 30 day Job Listing for FREE

In this Swift Tutorial I am going to show you how to detect when the user has made the device go into low power mode and to stay updated on the mode as it changes. I will show you how to accomplish this using a property from the class NSProcessInfo called: LowPowerModeEnabled and then listening to the state change using: NSProcessInfoPowerStateDidChangeNotification.

Swift Tutorial Detect Low Power Mode

As of present, the Xcode iOS Simulators do not have the option to enable/disable Low Power Mode. To test this project, you will need to put this on your device.

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.

Why detect low power mode

Scenario:

Let us assume that your app connects to a web-server every 15mins in the background. Like the iOS mail app (Depending on your settings). It then updates data, its crucial data that the user (We will call her… Linda) is expecting to show up, Linda uses the notifications to survive in a fast paced world, her world and her business depends on getting these notifications. But then Low power mode comes on and stops background activities. It’s ok for now, she knows that in 1 hour she will have a power supply and the low power mode will turn off. So, one hour later, she plugs in her device, sighs relief and goes about her day. Later that evening she meets a friend (Sarah) who has all the latest gossip, business news and is going away for the weekend because she found an amazing deal. Linda is confused because she didn’t get any of this information, she has missed out on a vital sale, a nearly ending Ebay item and a weekend away Groupon voucher for $29. Linda is mortified, she realises that after low power mode her app didnt come back on, didn’t start it’s usual background activity and overall made her experience a low, cold and hurtful one.

Don’t hurt people like Linda.

Now you are detecting the low power mode status, you can adjust your apps activity accordingly.

Linda likes this.

This tutorial assumes that you have a good grasp of Swift 2

Create Detect Low Power Mode Project

For this tutorial, we are going to create a basic app that will try to download a video from youtube. Not actually extract and download onto the device just load.We will detect low power mode using swift and defer the action until after the low power mode is deactivated.

Create a new Single View Application, call it whatever you like and save it wherever you see fit.

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:


var StartVideoDownload = true
let processInfo = NSProcessInfo.processInfo()

An NSProcessInfo object is created the first time this method is invoked, and that same object is returned on each subsequent invocation.

Now we are going to create a function that will start the download:


func downloadStart(){
    guard let url = NSURL(string: "https://www.youtube.com/embed/YQHsXMglC9A") where
       !processInfo.lowPowerModeEnabled else{
       return
    }
StartVideoDownload = false
}

This video is Adelle – Hello. Do not judge me.

Now, we are going to implement the powerModeChanged(_:) method.


func powerModeChanged(notif: NSNotification){
     guard mustDownloadVideo else{
         return
     }
downloadStart()
}

That’s it. A very simple solution to detecting low power mode in your apps and providing alternatives for downloading to the user.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


iOS-Blog Admin Team

Written by:

We're here to help.

Be the first to comment

Would you like to sign up to the mailing list by our sister site: SwiftMonthly?