问题
Do background mode functions like Background fetch and Location update work if the app is terminated? Or it only works if the app enters background?
Thanks
回答1:
Yes, it works (most of the time), if you set up everything correctly and have the permissions. Your app need's to be launched at least 1 time, so it can subscribe to the updates.
For background fetch, set UIApplication.shared.setMinimumBackgroundFetchInterval(3600)
at the didFinishLaunching
method, implement the performFetchWithCompletionHandler
method, and enable the Background Fetch in the Background Modes.
Pay attention to do it as quickly as possible, and call the completionHandler as soon as possible.
Read more on Updating Your App with Background App Refresh here
For notification updates, you must also set the allowsBackgroundLocationUpdates
property of your CLLocationManager
object to true
, and enable the Location updates in the Background Modes.
Read more on Handling Location Events in the Background here
回答2:
Background fetch
works like, it allows the app to download the contents when it is background. If the app is terminated and gets some trigger to download content, it will actually wake up by doing silent-launch of the app in the background and download the contents. Please see the Apple description on this below.
Each of the preceding modes lets the system know that your app should be woken up or launched at appropriate times to respond to relevant events. For example, an app that begins playing music and then moves to the background still needs execution time to fill the audio output buffers. Enabling the Audio mode tells the system frameworks that they should continue to make the necessary callbacks to the app at appropriate intervals. If the app does not select this mode, any audio being played or recorded by the app stops when the app moves to the background.
Here, preceding modes refer to "Background fetch, Audio and AirPlay, Location updates
and other Bacground modes of the app"
Please refer Apple document on Background Execution. See "Declaring Your App’s Supported Background Tasks
" for more info on different background modes.
Location update works differently. There are multiple Apple services available to fetch location.
Significant Location service: It works in all modes. Foreground, Background and even in terminated mode.
Standard Location service: It works only in FG and BG mode. It does not work when the app is in terminated mode.
On more details on Location in BG, please refer Handling Location Events in the Background document.
Hope it helps.
回答3:
Background fetch and Location update work if the app is terminated? Or it only works if the app enters background?
It depends on which type of location service
you have used in the project. Refer below analysis of all types of location services.
Standard location service: If you implemented standard location service then it will work only for
background
andforeground
state.Significant location updates: If you implemented significant location updates then it will work for
background
,foreground
andterminate
state as well.Region Monitoring: If you implemented significant location updates then it will work for
background
,foreground
andterminate
state as well.Visits Location Service: If you implemented Visits Location Service then it will work for
background
,foreground
andterminate
state as well.
Please refer below references.
Apple official doc
Raywenderlich article
来源:https://stackoverflow.com/questions/55740406/ios-background-services-when-app-is-terminated