satya
satya

Reputation: 241

How to save samples of type HKQuantityTypeIdentifierBodyMass in HealthKit in iOS

I try to get authorization to save samples of type HKQuantityTypeIdentifierBodyMass: and HKCharacteristicTypeIdentifierDateOfBirth

My code is,

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

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

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

when I'm running this code, I get the exception:

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

i am running in iOS 9.2 and Xcode 7.2. Any help is appreciated.

Upvotes: 2

Views: 486

Answers (1)

Akhilrajtr
Akhilrajtr

Reputation: 5182

As per the documentation of requestAuthorizationToShareTypes

typesToShare includes a set containing the data types you want to share. This set can contain any concrete subclass of the HKSampleType class

typesToRead includes a set containing the data types you want to read. This set can contain any concrete subclass of the HKObjectType class

so in your case,

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

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

either try,

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

or try

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

Upvotes: 2

Related Questions