HealthKit 数据在后台无法访问

HealthKit data inaccessible in background

我想使用后台提取在后台提取 HealthStore 数据,但我一直收到错误消息:Error Domain=com.apple.healthkit Code=6 "Protected health data is inaccessible" UserInfo=0x17026fdc0 {NSLocalizedDescription=Protected health data is inaccessible}

A​​pple 声明:

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.

有任何在后台访问数据的解决方法吗? HKObserverQuery 是后台获取数据的唯一方式吗?

正如您引用的文档中所述,当 phone 被锁定时,无法访问 HealthKit 数据。但是,如果 phone 已解锁且您的应用程序处于后台,则您可以访问数据。因此,如果在 phone 解锁时发生后台提取,您将能够访问数据。