Perform background tasks when app is terminated
You have two options
- Background App Refresh
- Silent push notifications
Easiest one is Background App Refresh. Because later one needs a server to send the notification. You can check following API for the usage. Basically you set Background Fetch capability on Capabilities/Background Modes of your app. Then from time to time, iOS will wake up your app and call application(_:performFetchWithCompletionHandler:)
delegate. You will have around 30-45 seconds to call your function and call completion handler. If you don't finish it on time, iOS will kill your app. If you don't obey the rules, iOS will give you less chances to wake up. For more detailed usage of Background Modes, you may check following tutorial
UWP in-process background task when app is closed
Turns out, contrary to the answer the condescending Microsoft employee provided, that the background task will run if the app is closed, even if it is implemented as an in-process background task.
If the background task is implemented as an in-process task, then the OnBackgroundActivated
method will be used as the entry point, in case the app is not running. Otherwise, the method will be called while the app is running.
The reason why there were problems with the background task starting was that I used a condition (namely the SystemConditionType.UserPresent
) on the task. This behaves very unpredictably. However, I tested (by waiting for several calls) both the in-process and out-of-process approach and the task gets run in both cases, even if the app is closed.
If a task deferral is used, the background task should not be terminated even if the user closes the app.
In conclusion, at the time of writing this post (October 17th, 2020) the documentation does not clearly explain this behavior and the aforementioned condescending misinterpretation by, ironically enough, Microsoft's own employee only proves that point.
Related Topics
iOS Nsdate() Returns Incorrect Time
Remove Grey Background on Link Clicked in iOS Safari/Chrome/Firefox
How to Create Custom Mkannotationview and Custom Annotation Title and Subtitle
Nstimer Timerwithtimeinterval: Not Working
What Is the Life Cycle of an iPhone Application
How to Create a Uilabel with Strikethrough Text
"Do Not Embed", "Embed & Sign", "Embed Without Signing". What Are They. What They Do
Swift Error:Signal Sigabrt How to Solve It
How to Password Protect Writing to Nfc Ntag216 Tag on iOS 13 Using Nfc Core
iOS 9 Safari: Changing an Element to Fixed Position While Scrolling Won't Paint Until Scroll Stops
How to Access File Included in App Bundle in Swift
Using Weak Self in Dispatch_Async Function
Tutorial on How to Drag and Drop Item from Uitableview to Uitableview
Keeping a Uibutton Selected After a Touch
iOS - Mkmapview Place Annotation by Using Address Instead of Lat/Long