HealthKit授权状态始终为1

我在我的应用程序中使用HealthKit。 我正在获取用户访问HealthKit数据的权限。 授权后,如果我检查特定HealthKit对象types的授权状态,则始终返回访问被拒绝。 (1是枚举整型值)。

这是我的代码

// Steps if ([self.healthStore authorizationStatusForType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]] == HKAuthorizationStatusSharingAuthorized) { [self accessStepsFrom:fromDate to:toDate]; } //Sleep if ([self.healthStore authorizationStatusForType:[HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis]] == HKAuthorizationStatusSharingAuthorized) { [self accessSleepFrom:fromDate to:toDate]; } //DOB if ([self.healthStore authorizationStatusForType:[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]] == HKAuthorizationStatusSharingAuthorized) { [self accessDOB]; } 

方法[self.healthStore authorizationStatusForType:[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]]总是抛出我1 。 需要帮助吗?

HKObjectType的授权状态并不反映您的应用程序是否有权读取这些types的样本。 它仅表示您是否已经请求授权,以及您的应用是否有权写入这些types的样本。 因此,如果您的应用请求授权读取步数样本但不写入,并且用户授予读授权,则HKQuantityTypeIdentifierStepCount的授权状态将为HKAuthorizationStatusSharingDenied。

以下是来自HealthKit框架参考,并解释了为什么您的应用程序可能不会查询它是否具有读取权限:

为了防止敏感的健康信息泄漏,您的应用程序无法确定用户是否授予了读取数据的权限。 如果您没有获得权限,则显示为在HealthKit存储中没有所请求types的数据。 如果您的应用具有共享权限但未具有读取权限,则只能看到应用已写入商店的数据。 其他来源的数据仍然隐藏。

 NSArray *readTypes = @[[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]]; [self.healthStore requestAuthorizationToShareTypes:nil readTypes:[NSSet setWithArray:readTypes] completion:nil];