Dheeraj Singh
Dheeraj Singh

Reputation: 5183

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

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!!!

Upvotes: 1

Views: 1115

Answers (2)

rmvz3
rmvz3

Reputation: 1173

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).

Upvotes: 1

lehn0058
lehn0058

Reputation: 20237

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
    // ...
}

Upvotes: 0

Related Questions