Optimising 🔋 battery while building iOS apps

Nikhil Manapure
The Swift Blog
Published in
4 min readJan 16, 2017

What is one thing we never like about using beautiful apps 🤔 … their battery usage. Most beautiful apps discharge the phone rapidly. And as now I am building app, I want it to be least battery consuming and here is how I am making that sure.

Points to remember —

  1. Try to run and test on variety of devices as results are bound to vary.
  2. There will be a saturation point and then you won’t be able to optimise the app more but there will be lot many places to optimise 💉 before reaching that point.
  3. The overall performance and draining will depend on
  • Display 📱
  • CPU 📲
  • Networking 📡
  • Location 📍
  • Background ⚙️

So here is how to analyse energy impact of your app, we will be using our old friend XCode only for this too. So where ever you are in XCode just press ⌘+6 for Debug Navigator or click on it’s icon or if you are so old school 😒 then go to View > Navigator > Show Debug Navigator.

I hope your app is running already or else go ahead and it. Now look at Debug Navigator.

Debug Navigator

You will see various gauges namely —

  1. CPU : Shows current and historical CPU usage with respect to threads
  2. Memory : Shows RAM usage
  3. Disk : Shows total disk read and write activity along with speed
  4. Network : Shows inbound and outbound network activity
  5. Energy Impact : Our guy who tells about 💪

So concentrating on our guy, once we open it, we can see this

Energy Impact gauge

Here we can see if utilisation is high or low, lower the better. We also see historic usage which is on per second basis. In historic graph, blue is good, red is bad. Blue is normal cost while red is overhead. So if you see something like this in your app then you should try finding causes of overheads.

Energy Impact graph per second

Now how you can do this is by checking functionalities one by one and also analysing its energy impact graph simultaneously. And whenever you see something red just look at if this overhead was generated due to network, CPU, location or GPU. In this way you can vaguely find out cause for your overheads but this is not enough, Apple provides this unique tool called as Instruments.

On Energy Impact gauge we can see three buttons named - “Location”, “Time Profile” and “Network Profile”. These three buttons end up by starting analysis of your app on Instruments in different profiles. There we can even pin point to the method causing extra resource usage. This can be used to analyse the app more keenly. I will cover Instruments in whole different article.

And of course the last analysis you should always do for checking your apps’ battery usage is from device itself from battery settings which has two segments where we can see battery usage in last 24 hours and in last 7 days. We can also get to know how much time app was in foreground and also in background. This should be last test performed in beta state before 🚀 .

Happy Coding and always remember -

The sooner you start coding, the longer it takes.

--

--