Shawn  Durgan

Shawn Durgan


Swift iOS BackgroundTasks framework — Background App Refresh in 4 Steps

IOS allows app to refresh it content even when it is sent to background. iOS can intelligently study the user’s behaviour and schedule background tasks to the moment right before routine usage. It is useful for app to retrieve the latest information from its server and display to user right when app is resumed to foreground. Examples are social media app (Facebook, Instagram & WhatsApp) and news app.

Image for post

illustration of foreground and background task; Copy from WWDC 2019 presentation slide

Since iOS 13, Apple has added a new framework — BackgroundTasks — to handle the background task request between iOS and app side. BGAppRefreshTask & BGProcessingTask are the two available types of background task. BGAppRefreshTask can have 30 seconds to complete its job and BGProcessingTask can have more than a minute. The handling method is different for the old application(_:performFetchWithCompletionHandler:) way.

Background task execution depends on several conditions, including:

  1. User permission on “Background App Refresh”
  2. Power supply from charger
  3. Network connectivity

Let’s see how to setup the Background App Refresh and understand the details on using it!

4 Steps to Background App Refresh

Step 1) Enable Background Modes for app target

Go to the “Background Modes” session of the app target and check “Background fetch” and “Background processing” options if BGAppRefreshTask and BGProcessingTask are used respectively.

Image for post

#ios-app-development #background-task #swift #background #ios

Swift iOS BackgroundTasks framework — Background App Refresh in 4 Steps