Accessing Health Kit data into Apple Watch OS 2 excluding Workout Data

1k Views Asked by At

I am able to access Workout data using workout session but unable to do the same with others such as accessing Height,Weight,Dietary Water, Body Temperature,Blood Pressure etc.

Also i am able to access heart rate but unable to access body temp. Both of them are same vital sign identifiers.

Is it that watch can access only Workout data as mentioned in WWDC 2015 video?

enter image description here

Sample Code:

-(void)bodyTempForLabel :(WKInterfaceLabel *)bodyTempLabel {

    HKSampleType *bodyTemp = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyTemperature];

    [self readMostRecentSampleType:bodyTemp withCompletion:^(HKQuantitySample *quantitySample, NSError *error) {

        if(error) {

            NSLog(@"Error Reading Weight From health Kit");
        }

        self.bodyTemp = quantitySample;

        double bodyTempinDegree = [[self.bodyTemp quantity] doubleValueForUnit:[HKUnit unitFromString:[NSString stringWithFormat:@"%@C", @"\u00B0"]]];

        dispatch_async(dispatch_get_main_queue(), ^{

            [bodyTempLabel setText:[NSString stringWithFormat:@"%f",bodyTempinDegree]];
        });

    }];
}

-(void)readMostRecentSampleType : (HKSampleType *)sampleType withCompletion:(void(^)(HKQuantitySample *quantitySample,NSError *error))recentSample {

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];

    HKQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:nil limit:HKObjectQueryNoLimit sortDescriptors:@[sortDescriptor] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {

        if(!error) {
            // No results retuned array empty
           HKQuantitySample *mostRecentSample = results.firstObject;

            recentSample(mostRecentSample,error);
        }

    }];

    [_healthStore executeQuery:sampleQuery];

}

Any help would be appreciated. Thanks!!!

2

There are 2 best solutions below

1
On

The apple watch has access to all health kit types (though only a subset of the data). Has your app asked for permission for all of those types? Each type you want to read or write needs to be explicitly asked for when you setup your health store. For example, to read energy burned, distance, and heart rate you need to include:

let typesToRead = Set([
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
])

self.healthStore.requestAuthorizationToShareTypes(typesToShare, readTypes: typesToRead) { success, error in
    // ...
}
2
On

It seems you'll need to use a real device to debug. I'm unable to get any value from HK when running the simulator but it works fine in the Apple Watch. (Using XCode 7 Beta 5).