iOS Background Execution Limits

This thread has been locked by a moderator.

I regularly see questions, both here on DevForums and in my Day Job™ at DTS, that are caused by a fundamental misunderstanding of how background execution works on iOS. These come in many different variants, for example:

  • How do I keep my app running continuously in the background?

  • If I schedule a timer, how do I get it to fire when the screen is locked?

  • How do I run code in the background every 15 minutes?

  • How do I set up a network server that runs in the background?

  • How can my app provide an IPC service to another one of my app while it’s in the background?

The short answer to all of these is You can’t. iOS puts strict limits on background execution. Its default behaviour is to suspend your app shortly after the user has moved it to the background; this suspension prevents the process from running any code.

There’s no general-purpose mechanism for:

  • Running code continuously in the background

  • Running code at some specific time in the background

  • Running code periodically at a guaranteed interval

  • Resuming in the background in response to a network or IPC request

However, iOS does provide a wide range of special-purpose mechanisms for accomplishing specific user goals. For example:

  • If you’re building a music player, use the audio background mode to continue playing after the user has moved your app to the background.

  • If you’re building a timer app, use a local notification to notify the user when your timer has expired.

  • If you’re building a video player app, use AVFoundation’s download support.

Keep in mind that the above is just a short list of examples. There are many other special-purpose background execution mechanisms, so you should search the documentation for something appropriate to your needs.

IMPORTANT Each of these mechanisms fulfils a specific purpose. Do not attempt to use them for some other purpose. Before using a background API, read clause 2.5.4 of the App Store Review Guidelines.

Additionally, iOS provides some general-purpose mechanisms for background execution:

All of these mechanisms prevent you from abusing them to run arbitrary code in the background. As an example, consider the NSURLSession resume rate limiter.

For more information about these limitations, and background execution in general, I strongly recommend that you watch WWDC 2020 Session 10063 Background execution demystified. It’s an excellent resource.

Finally, if you have questions about background execution that aren’t covered by the resources listed here, please open a new thread on DevForums with the details. Tag it appropriately for the technology you’re using; if nothing specific springs to mind, use BackgroundTasks. Also, make sure to include details about the specific problem you’re trying to solve because, when it comes to background execution, the devil really is in the details.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Change history:

  • 22 Jul 2021 — First posted.

  • 26 Jul 2021 — Extended the statement about what’s not possible to include “running code periodically at a guaranteed interval”.

  • 12 Aug 2021 — Added more entries to the common questions list, this time related to networking and IPC. Made minor editorial changes.

Post not yet marked as solved Up vote post of eskimo
549 views