首页 > 解决方案 > 有没有办法从 Healthkit 获得多个数据点的集体许可

问题描述

我正在创建一个需要从 HealthKit 获取 heartRate、restingHeartRate、walkingHeartRateAverage、HeartRateVariabilitySDNN 数据的应用程序。

我还想获取用户的身体测量数据。即体重指数、体温、身高、腰围、体重。

要获得读取权限,我需要为每种数据类型获得单独的权限:

let healthKitTypesToRead: Set<HKObjectType> = [
    HKObjectType.quantityType(forIdentifier: .heartRate)!,
    HKObjectType.quantityType(forIdentifier: .heartRateVariabilitySDNN)!,
    HKObjectType.quantityType(forIdentifier: .restingHeartRate)!,
    HKObjectType.characteristicType(forIdentifier: .walkingHeartRateAverage)!,
    HKObjectType.quantityType(forIdentifier: .bodyMass)!,
    HKObjectType.quantityType(forIdentifier: .bodyMassIndex)!,
    HKObjectType.quantityType(forIdentifier: .bodyTemperature)!,
    HKObjectType.quantityType(forIdentifier: .height)!,
    HKObjectType.quantityType(forIdentifier: .waistCircumference)!,
    HKObjectType.quantityType(forIdentifier: .weight)!,
] 

它在权限屏幕上显示了多个切换,我觉得对于用户来说,给每个单独的权限都太多了。(以防他们不想授予所有权限。)

有没有办法对权限进行分组,或者是否有一些数据类型,如果给予权限,我们也可以收集其他数据类型。

最初我认为允许 heartRate 意味着我可以获取 restingHeartRate、walkingHeartRateAverage 等。但它们都需要各自的权限。

标签: iosswifthealthkit

解决方案


每个 HealthKit 类型必须独立授权:

为了帮助保护用户的隐私,HealthKit 需要细粒度的授权。在尝试访问或保存数据之前,您必须请求读取和共享应用程序使用的每种数据类型的权限。

https://developer.apple.com/documentation/healthkit/authorizing_access_to_health_data

Apple 预见到一次请求多个 HealthKit 类型的困难,并建议在您的应用程序中的不同时间而不是一次全部请求不同权限类型的权限:

但是,您不需要一次请求所有数据类型的权限。相反,等到您需要访问数据后再请求许可可能更有意义。

HealthHit 权限对话框也有一个按钮,允许用户启用所有请求的类型:

打开所有类别

打开所有类别


推荐阅读