A class for registering launch handlers that are run by submitting task requests to launch your app in the background.
- iOS 13.0+Beta
- Mac Catalyst 13.0+Beta
- tvOS 13.0+Beta
A task is a standalone activity, such as cleaning a database, updating a machine learning model, or updating the displayed data for an app. To make efficient use of processing time and power, the system can launch your app in the background to run tasks like these when the device isn’t in use.
First you need to provide a launch handler, a block of code that performs a task. During app startup, register a launch handler and an associated unique identifier for each task. Submit a task request for the system to launch your app in the background at a later time to run the launch handler.
Running a task can happen many times and requires:
Configuring the app to enable background task execution.
Registering a launch handler for the task.
Scheduling the task.
A task object can specify an expiration handler and can inform the system when the associated task is complete.
Configure the App for Background Tasks
Configure the app for background tasks by adding the capabilities for the required background modes, and by adding a whitelist of task identifiers.
To add the capabilities:
Open the project editor and select the desired target.
Click Signing & Capabilities.
Expand the Background Modes section. If there’s no Background Modes, click the “+ Capability” button and choose Background Modes in the window that appears.
Select one or both of Background fetch and Background processing based on the needs of your background tasks.
The system runs only tasks registered with identifiers on a whitelist of task identifiers. To add the whitelist, add the identifiers to the
Info file (The Info.plist File):
Open the Project navigator and select the desired target.
Click Info and expand Custom iOS Target Properties.
Add a new item to the list and choose “Permitted background task scheduler identifiers”, which corresponds to the
Scheduler Permitted Identifiers
Add the string for each authorized task identifier as a separate item in the array.
Register and Schedule Tasks
You must register all of the launch handlers before
application completes and there must be one launch handler for each identifier in the
Schedule a registered task at any time using
submit(_:). Submitting a task again replaces the previous submission. Updating the settings of a scheduled task requires resubmitting the task.
Use Tasks in Extensions
Extensions can schedule tasks, such as the update of a machine learning model. Register tasks scheduled by extensions in the main app. The system launches the app to run the task.