Swift iOS Background App Refresh in 3 Steps

iOS allows app to refresh it content periodically even when it is sent to background. It is usefully 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.
However, this background app refresh has a lot of limitations, including:
- User permission
- Period of refresh
- Duration of each refresh execution (max 30 seconds)
Let’s see how to setup the Background App Refresh and understand the details and tricks on using it!
3 Steps to Background App Refresh
- Check “Background fetch” option at the “Background Modes” session of the app target tab

2. Set the minimum interval of app refresh at the start of app (didFinishLaunchingWithOptions
)
Bear in mind, iOS has the privilege to control the exact time interval of app refresh! This parameter just tells iOS the highest frequency that app would expect for.
3. Implement the application(_:performFetchWithCompletionHandler:)
method in AppDelegate.swift
.
Simulating a Background App Refresh by Xcode
Waiting an indefinite moment of time for iOS to trigger a Background App Refresh is not worthy. To save time, Xcode provides a feature to simulate a Background App Refresh on both simulator and real device.
Please go to the Debug tab and click the “Simulate Background Fetch”.

Background App Refresh Details
- iOS does NOT guarantee the exact period of app refresh. It only fires an event to
performFetchWithCompletionHandler
when the network is in good condition and device is not running in Low-Power mode. This can ensure the data fetching is likely to be succeed. - The
completionHandler
should be called as soon as possible inside theperformFetchWithCompletionHandler
callback. This is because a lengthy process consumes more power at all. In order to provide a better UX to user, Apple would reschedule the app refresh and those app requires lengthy process would be refreshed less frequently. - The maximum duration of background app refresh is limited to 30s. We can test it by logging the
UIApplication.shared.backgroundTimeRemaining
out at theperformFetchWithCompletionHandler
.
Monitor Background App Refresh authorization

User has the final privilege to enable / disable the “Background App Refresh” even both network and battery situation are at good level. Developer should monitor the corresponding authorization and redirect user to Settings app if background app refresh is a critical feature.
We can use UIApplication.shared.backgroundRefreshStatus
to get the current authorization status of Background App Refresh. There are three possible values:
available
—User has enable the Background App Refresh feature. This is the default value when app is first installed.denied
— User has explicitly disabled the Background App Refresh feature. We can redirect user to the app settings page to toggle back the status with codeUIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
.restrict
— User is not able to toggle the background refresh status due to his account restriction, e.g. parental control
Monitoring the change of authorization status
After user has changed the Background App Refresh status, app can be notified by observing the UIApplication.backgroundRefreshStatusDidChangeNotification
notification. This can be used to show any pop up dialog to user to explain the usage and importance of Background App Refresh to the app flow.
Things you probably like:
If you have to support user with device running in iOS 13 or above, use the newly introduced BackgroundTasks framework instead. You can find more information here below:
Further readings & References:
1. Apple Official Documentation — Updating Your App with Background App Refresh
2. Raywenderlich Tutorial on Background Modes
You are welcome to follow me at Twitter@myrick_chow for more information and articles. Thank you for reading this article. Have a nice day! 😄