State Preservation and Restoration 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. In the case of the home security app described above, the system would monitor the connection request, and re-relaunch the app to handle the centralManager:didConnectPeripheral: delegate callback when the user returned home and the connection request completed.
How can I trigger this and test the code?
I have got an accessory with a service. I have got an app that scans for the service and I opted in state preservation. However I am not sure how to logically test it as I do not know what I need to trigger it. These are the options that I tried unsuccessfully:
A - kill the app from Xcode
B - kill the app manually
C - power off the phone
D - something else
In all these options I tried to go to Xcode -> device and look at the logs, but haven't seen any state restoration logs.
Thanks
NB Thanks to user1785784 for sharing Apple's QA1962 - Conditions Under Which Bluetooth State Restoration Will Relaunch An App which describes new Bluetooth behaviour in iOS 11. This document should be considered the answer to this question, although I think it incorrectly claims that iOS 10 would relaunch an app that has been force quit. (I haven't tested that on an iOS 10 device, but it would have been a departure from iOS 9. Can anyone confirm?).
Killing the app manually (B) from the task switcher, ensures your app will not be launched automatically until the user explicitly opens it again.
C doesn't work either, I think only VOIP apps are launched automatically after restart, and then only after the device is unlocked.
I don't know any D.
I use A.
First, to implement Bluetooth State Restoration, make sure you've
bluetooth-central
as aUIBackgroundModes
to your Info.plistCBCentralManagerOptionRestoreIdentifierKey
when initing yourCBCentralManager
-(void)centralManager:willRestoreState:
callback in yourCBCentralManager
delegate.Then you're ready to test state restoration:
NB:
application:didFinishLaunchingWithOptions:
will be called first, and you must immediately init yourCBCentralManager
as described above. ThencentralManager:willRestoreState:
will be called.Just figured this out recently with the help of Apple Tech. Also given/have a nice link that shows the different ways to cause the app to restart without user intervention.
I did it by causing the app to crash suddenly with the following snippet of swift code. This causes the app to restart and the call the 'willRestoreState' callback.