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.

Instructions

To be able to test Background tasks, please, follow these steps:

  1. Add the following key to Info.plist to enable Background modes:

     <key>UIBackgroundModes</key>
     <array>
     	<string>fetch</string>
     	<string>processing</string>
     </array>
    
  2. 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.

  3. 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.)
  4. Once attached to Xcode, send the app to background and open it again; this, to register the background tasks in the OS.

  5. On Xcode, enable the lldb in the debugger by pausing the debug session.

  6. In the debugger, execute the line shown below, substituting the identifier of the desired task for TASK_IDENTIFIER:

     e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"TASK_IDENTIFIER"]
    
  7. Resume your app. The system launches the app in the background to run the desired task.

  8. To force termination of a task, execute the line shown below, substituting the identifier of the desired task for TASK_IDENTIFIER.

     e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateExpirationForTaskWithIdentifier:@"TASK_IDENTIFIER"]
    
    
  9. Resume your app. The system calls the expiration handler for the desired task.

For more info, visit the following link.

Build Requirements

Building this sample requires Xcode 11.0 and iOS 13.0 SDK

Refs

Target

This sample runnable on iPhone/iPad

License

Xamarin port changes are released under the MIT license.

The original sample is released under the following license.