HealthKit data inaccessible in background
Asked Answered
M

1

11

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?

Milliary answered 20/5, 2015 at 21:52 Comment(0)
P
9

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.

Progress answered 21/5, 2015 at 1:31 Comment(7)
If this is limiting your app's abilities you should file a radar (I've filed one about this).Progress
Specifically, you can register for background delivery of data using - enableBackgroundDeliveryForType:frequency:withCompletion: on HKHealthStore.Eradis
Also note that using enableBackgroundDeliveryForType:frequency:withCompletion: will still have the limitation where HealthKit data is not accessible while the phone is locked. @tshortli would know the best, but I don't think there's any workaround for this.Progress
It isn't something you should be trying to work around. HealthKit data is encrypted at rest intentionally using iOS Data Protection. When the phone is locked, nothing can access HealthKit data because it cannot be decrypted until the user enters their passcode. See apple.com/business/docs/iOS_Security_Guide.pdf for more information about Data Protection.Eradis
@tshortli Thanks for the confirmation and info :) Unfortunately, this severely limits functionality in certain apps which can have a detrimental affect on customer satisfaction although I understand why it may be necessary (unfortunate). See rdar://20011490Progress
I'm running into an occasional (not every time) error when attempting to load workout data from HealthKit on the iPhone: Error Domain=com.apple.healthkit Code=6 "Protected health data is inaccessible" {NSLocalizedDescription=Protected health data is inaccessible} The only other references to this error I've found is when attempting to access HealthKit data when the phone is locked But I am attempting to load the data with the phone unlocked. Any other idea what the issue could be?Repatriate
@AdeeshJain I’m not sure why you would get that error if the phone is unlocked. If you able to pair down the code to a simple sample project then post a new SO question or file a radar you may have more luck finding an answer.Progress

© 2022 - 2024 — McMap. All rights reserved.