Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Latest commit

 

History

History

check_background_refresh_setting

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
id title brief
DACC6699-76BF-4F8C-8C72-AFA7021B3A00
Check Background Refresh Settings
The Background App Refresh Setting gives the user more control over what our application can do in the background. Let's see how changing this setting affects the backgrounded behavior of our location app.

Recipe

In this example, we will use the BackgroundRefreshStatus to check whether background refresh is available for an application.

  1. First, add the following code to the OnActivated method in the AppDelegate class. This will notify us of the application's background refresh status:
if (application.BackgroundRefreshStatus == UIBackgroundRefreshStatus.Available) {
  Console.WriteLine ("Background refresh available");
} else {
  Console.WriteLine ("Background refresh not available");
}
  1. Deploy the application to device. This should print "Background refresh available" to the console, indicating that the application can operate in the background after the user has exited:
![]("Images/bg_start.png")
  1. Let's see what happens when we change the refresh setting. While the app is running in the background, navigate to **Settings > General > Background App Refresh** on the device:
![]("Images/3_.png")
  1. Find the location application in the list, and remove backgrounding privileges:
![]("Images/4_.png")
  1. Run the application and put it in the background. The background location updates will stop:
![]("Images/bg_stop.png")

And the application output will print the updated status:

Note that the application is not terminated, since the debugger remains connected. An application with the background refresh app setting turned off is simply suspended by the operating system as soon as it enters the background, and cannot do any background processing.

We can also track when the background refresh setting changes by subscribing to ObserveBackgroundRefreshStatusDidChange notifications. This will be called when the application enters the foreground, after the user changes the setting:

UIApplication.Notifications.ObserveBackgroundRefreshStatusDidChange ((sender, args) => {
    Console.WriteLine("Background refresh status changed");
  });