On iOS, there are two ways an app is completely terminated:-
- System terminates the app (because for example the app is not used for a long time and the system kills it to save memory).
- The user terminates/kills the app.
For the first scenario, you need to add support for State Preservation and Restoration. From Documentation Archive from Apple:-
Because state preservation and restoration is built in to Core
Bluetooth, your app can opt in to this feature to ask the system to
preserve the state of your app’s central and peripheral managers and
to continue performing certain Bluetooth-related tasks on their
behalf, even when your app is no longer running. When one of these
tasks completes, the system relaunches your app into the background
and gives your app the opportunity to restore its state and to handle
the event appropriately.
Basically, you need to add code to support this feature as it is not enabled by default. Please have a look at the link below, specifically the section "Adding Support for State Preservation and Restoration":-
Core Bluetooth Background Processing for iOS Apps
For the second scenario, if the user terminates/kills the app, then there is no way for the app to restore or relaunch itself. There is a security and behavioural reason for this in that if there is a bug or an issue and you force killed the app, you wouldn't want your app to somehow relaunch itself, because otherwise you'll end in an infinite loop and you will never be able to fully terminate the buggy app. Please have a look at the following link that further explains this:-
https://www.raywenderlich.com/1395-state-restoration-tutorial-getting-started
https://medium.com/arkulpa/ios-stay-connected-to-an-external-ble-device-as-much-as-possible-699d434846d2
This is also mentioned indirectly in the Documentation Archive:-
Apps that support background execution may be relaunched by the system
to handle incoming events. If an app is terminated for any reason
other than the user force quitting it, the system launches the app
when one of the following events happens...
I hope this helps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…