iOS 如何在 HealthKit 中保存 HKQuantityTypeIdentifierBodyMass 类型的样本

How to save samples of type HKQuantityTypeIdentifierBodyMass in HealthKit in iOS

我尝试获得授权以保存 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 中 运行。感谢任何帮助。

根据 requestAuthorizationToShareTypes

的文档

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

的任何具体子 class

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

的任何具体子 class

所以在你的情况下,

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];