How to kill your app when it enters background mode

By default, any iOS app we create with Xcode automatically continues running for a short period of time, even if the user puts it in the background and launches a new app. You can even prolong the background execution of an app if your app needs it (for example, finish downloads or long running calculations).

But not all apps need this capability. In fact, if we want to be really nice, we can opt out app in to terminate when it hits the background. It’s really easy to do too: all we need to add is a boolean value into our Info.plist file. It’s called UIApplicationExitsOnSuspend.

Set it to YES and our app terminates as soon as someone presses the home button.

Screen Shot 2015-11-06 at 23.26.02

To add the key, either select your Info.plist file, or head over to your target and click the Info tab at the top (see screenshot).

This is a plist editor, and if you’re not familiar with how to use it, let me introduce you to one of the worst examples of UI design ever – Apple Style:

  • hover over the Key column until you see those little +/- icons come up
  • select the plus icon to add another row
  • double-click the Key part if your row until you can type something in
  • type “UIApplicationExistsOnSuspend” and see it turn into the text “Application does not run in background mode”
  • notice that the type column has changed into Boolean
  • in the Value column, on the far right, find two arrows and change the value to YES
  • that’s it!

Termination Testing

The easiest way to test if this is working is to add a couple of log messages to your AppDelegate file. Find the method stubs for applicationDidEnterBackground and applicationWillTerminate and write yourself a message:

The first method will be called with or without the magic termination key. But the second method will only be called if the app is purged from memory. If you set the key correctly, both methods should be called.

Note that even though the app is killed, it is still visible when you double-tap the home key – but it no longer occupies any memory, nor will it continue to run in the background. As far as I know, this behaviour can only be changed by the user swiping up on an app after a double-tap, or by a hard reset of iOS (hold CTRL+HOME until the Apple Loog appears).

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

7 thoughts on “How to kill your app when it enters background mode

  1. Great tip Jay, but here is a more difficult issue that you may be able to resolve.

    How do I restore NSLog to console after applicationWillTerminate is called in the simulator.

    Try it and you will see that NSLog stops displaying to console in XCode, once you have terminated the app & relaunched.

    Any ideas?

    1. Hi Charles,

      I guess that’s expected behaviour. Terminating the app from within the app is like stopping it in Xcode, which means the connection between Xcode and the running app is broken. When you relaunch the app from the device, Xcode does not know about it and hence there’s no communication between the device and Xcode. The log output is still written to the device though, it’s just not displayed dynamically in Xcode.

      1. Jay. That’s a great explanation, but “where there is a will, there is a way…”

        Apple should be able to build this functionality into XCode, maybe as a preference.

        1. You’re absolutely right, and you’re in luck too, because Apple have half-implemented something like that. Here’s what you can do:

          • plugin your device into your Mac (only works with real devices, not the simulator)
          • open Xcode
          • navigate to Window – Devices, then select your device

          Xcode will now read the device’s log file and display it (remember the log file contains every message any system service or app wants to write in there, including our own NSLog messages). As you use your device, you can see new messages displayed in real time.

  2. Excellent article!! one more info which I am looking for is, whether the iOS app can be killed on pause programatically, i.e based on app’s condition (like what it is doing at the moment).
    Can this be done?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.