HealthKit get unit

Discussion in 'iOS Programming' started by erdinc27, Jun 21, 2017.

  1. erdinc27 macrumors regular

    Jul 20, 2011
    I pull many data from HealthKit including nutritious, workouts, body mass, weight, height. I can get them all without any problem but what I want to do is to get those data's units as well. For example, if the user uses height property in meter or the inch or any other metric unit. I am doing in this way
    lastHeight = [quantity doubleValueForUnit:[HKUnit meterUnit]];
    it gives the right height number but if the user uses in inch unit I still display it in meters. How can I pull units as well for the appropriate property?
  2. chown33 macrumors 604

    Aug 9, 2009
  3. erdinc27 thread starter macrumors regular

    Jul 20, 2011
    @chown33 thanks for the reply. I implemented the code now but it hits always error clause. I think it occurs because of the permission. How can I get permission from the user? Here how the code looks like
    - (void) getHeightUnitWithHander:(void (^)(BOOL isSuccessful, NSString *errorText, HKQuantity *heightUnit)) handler {
        NSSet *heightSet = [NSSet setWithObject:HKQuantityTypeIdentifierHeight];
        [self.healthStore preferredUnitsForQuantityTypes:heightSet completion:^(NSDictionary<HKQuantityType *,HKUnit *> *preferredUnits, NSError *error) {
            if (error) {
                NSLog(@"Error Unit %@", error.description);
            } else {
                NSLog(@"HEIGHT UNİTS %@", preferredUnits);

Share This Page