Search code examples
iosiphoneswiftswift2healthkit

Checking healthkit data periodically in background swift


I am programming an inactivity alert into my HealthKit-enabled iOS app. When a user has taken less than 100 steps in the last 60 minutes, based on data in Health, it sends a notification. This works perfectly when the app is open, but I am having some trouble making it work in the background. I have tried fetching steps data along with the background location-checking program described in this tutorial (https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial), but for some reason location data stops being collected in the background when the tutorial's program is run on a real iPhone.

So, my question is: How can my app reliably check Health data every minute in the background, even if the app has been closed for days?


Solution

  • The HealthKit database is encrypted when the device is locked. This means you are not able to read any data from it at all when the device requires you to enter your passcode/fingerprint (which the majority of devices have enabled). So unfortunately, even if you are able to get something to run in the background every minute you would not be able to read the data (your query will just return an error instead of any results).

    However, step data can still be accessed from the pedometer (this data is not encrypted). I recommend you look at using that instead of HealthKit for any background processing.

    Now it sounds like you really only need to check if the user has taken steps in the last hour. It would be much more efficient if you would only check that often instead of every minute. If your app has access to a server with push notifications setup, you could schedule a silent push notification to wake up your app in the background and do the step check from the pedometer once an hour.