Xamarin.iOS - Refreshing And Maintaining Your App Using Background Tasks
This is a Xamarin port of Apple's Refreshing And Maintaining Your App Using Background Tasks sample.
Use scheduled background tasks for refreshing your app content and for performing maintenance.
To be able to test Background tasks, please, follow these steps:
Add the following key to Info.plist to enable Background modes:
<key>UIBackgroundModes</key> <array> <string>fetch</string> <string>processing</string> </array>
Add the following key and uniques task ids to Info.plist:
<key>BGTaskSchedulerPermittedIdentifiers</key> <array> <string>com.xamarin.ColorFeed.refresh</string> <string>com.xamarin.ColorFeed.cleaning_db</string> </array>
You need to add a unique task id into Info.plist for each background task you want to execute.
To be able to test a background task, you need to the debug the app with Xcode by:
- Following these steps
- or by running the app on Visual Studio with Debug configuration and attaching the process to Xcode by clicking on Debug menu > Attach to process > ColorFeed (or the app name.)
Once attached to Xcode, send the app to background and open it again; this, to register the background tasks in the OS.
On Xcode, enable the
lldbin the debugger by pausing the debug session.
In the debugger, execute the line shown below, substituting the identifier of the desired task for
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"TASK_IDENTIFIER"]
Resume your app. The system launches the app in the background to run the desired task.
To force termination of a task, execute the line shown below, substituting the identifier of the desired task for
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateExpirationForTaskWithIdentifier:@"TASK_IDENTIFIER"]
Resume your app. The system calls the expiration handler for the desired task.
For more info, visit the following link.
Building this sample requires Xcode 11.0 and iOS 13.0 SDK
- Original sample page.
- Starting and Terminating Tasks During Development
- This sample code project is associated with WWDC 2019 session 707: Advances in App Background Execution.
This sample runnable on iPhone/iPad
Xamarin port changes are released under the MIT license.
The original sample is released under the following license.