I want to pull HealthStore data in the background using background fetch but I keep getting an error: Error Domain=com.apple.healthkit Code=6 "Protected health data is inaccessible" UserInfo=0x17026fdc0 {NSLocalizedDescription=Protected health data is inaccessible}
.
Apple states:
Because the HealthKit store is encrypted, your app cannot read data from the store when the phone is locked. This means your app may not be able to access the store when it is launched in the background.
Any workaround to access data in the background? Is the HKObserverQuery
the only way to get data in the background?
HealthKit can only be used for iOS phones. If you are interested in HealthKit please go to Settings and select Enable HealthKit. On the next page you'll be asked to allow GeneralHealth to access your health data. HealthKit will be enabled when you tap 'Enable Healthkit' at the bottom of the screen.
HealthKit is the developer framework behind it that allows apps to work with Apple Health and each other. If you're confused about how the Apple Health app works, what type of information you need to get the most out of it, and which apps are compatible, keep reading.
To quote: This data is stored in Data Protection class Protected Unless Open. Access to the data is relinquished 10 minutes after device locks, and data becomes accessible the next time user enters their passcode or uses Touch ID or Face ID to unlock the device.
HealthKit provides a central repository for health and fitness data on iPhone and Apple Watch. With the user's permission, apps communicate with the HealthKit store to access and share this data.
As stated in the documentation you quoted there's no way to access HealthKit data while the phone is locked. However, you can access the data if the phone is unlocked and your app is in the background. So if background fetch happens while the phone is unlocked you will be able to access the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With