Users of this app can choose whether they would like to use Healthkit features (they are not necessary for the app to work). I would like to set up an observer query to send data to the server in the background.
According to Apple's documentation at this link: https://developer.apple.com/documentation/healthkit/hkobserverquery/executing_observer_queries
If you plan on supporting background delivery, set up all your observer queries in your app delegate’s application:didFinishLaunchingWithOptions: method. By setting up the queries in application:didFinishLaunchingWithOptions:, you ensure that the queries are instantiated and ready to use before HealthKit delivers the updates.
From my understanding, setting up your observer queries in the application:didFinishLaunchingWithOptions: method means that I would have to prompt users for permissions at the first launch. Is it possible to set up background delivery from another place? For example, could I do so at the same time as I already prompt users for permissions?
Thanks!
One possible flow is:
In didFinishLaunching
, check UserDefaults
for a Boolean that indicates the user has approved HealthKit integration. If it is true
then call a function to set up your observer queries.
At the point in your app execution where the user indicates that they want to provide access to HealthKit data, prompt for permission and then set the Boolean in UserDefaults
once you have permission.
At this point you would also call your function to set up your observer queries so that you start receiving data.