OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 iOS 的 HealthKit 中保存 HKQuantityTypeIdentifierBodyMass 类型的样本 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:11
标题: ios - 如何在 iOS 的 HealthKit 中保存 HKQuantityTypeIdentifierBodyMass 类型的样本

我尝试获得授权以保存 HKQuantityTypeIdentifierBodyMass:HKCharacteristicTypeIdentifierDateOfBirth 类型的样本

我的代码是,

NSArray *readTypes = @[[HKObjectType   
    characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]];

NSArray *writeTypes = @[[HKObjectType 
    quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]];

[self.healthStore requestAuthorizationToShareTypes:[NSSet setWithArray:readTypes]
    readTypes:[NSSet setWithArray:writeTypes] completion:nil];

当我运行这段代码时,我得到了异常:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Authorization to share the following types is disallowed: HKCharacteristicTypeIdentifierDateOfBirth’.

我在 iOS 9.2Xcode 7.2 中运行。任何帮助表示赞赏。



Best Answer-推荐答案


根据 requestAuthorizationToShareTypes 的文档

typesToShare 包括一个包含您要共享的数据类型的集合。该集合可以包含 HKSampleType

的任何具体子类

typesToRead 包括一个包含您要读取的数据类型的集合。该集合可以包含 HKObjectType

的任何具体子类

所以在你的情况下,

NSArray *readTypes = @[[HKObjectType   
    characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]];

NSArray *writeTypes = @[[HKObjectType 
    quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]];

要么尝试,

[self.healthStore requestAuthorizationToShareTypes:[NSSet setWithArray:writeTypes]
    readTypes:[NSSet setWithArray:readTypes] completion:nil];

或者试试

NSArray *readTypes = @[[HKObjectType   
    characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth], [HKObjectType 
    quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]];
[self.healthStore requestAuthorizationToShareTypes:nil
    readTypes:[NSSet setWithArray:readTypes] completion:nil];

关于ios - 如何在 iOS 的 HealthKit 中保存 HKQuantityTypeIdentifierBodyMass 类型的样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36326003/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4