Search code examples
iosswifthealthkit

How to get metadata from HealthKit?


I'm working on an application that reads different health data from HealthKit application.

So far I managed to get the DOB, most recent records of height, weight and blood glucose.

What I still need is how to get the metadata for these objects, specifically I need to get the date/time the record was entered.

For example, to get the record of the height, I'm using this method:

func updateHeight()
{
// 1. Construct an HKSampleType for Height
let sampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)

// 2. Call the method to read the most recent Height sample
self.healthManager?.readMostRecentSample(sampleType, completion: { (mostRecentHeight, error) -> Void in

  if( error != nil )
  {
    println("Error reading height from HealthKit Store: \(error.localizedDescription)")
    return;
  }

  var heightLocalizedString = self.kUnknownString;
  self.height = mostRecentHeight as? HKQuantitySample;
  // 3. Format the height to display it on the screen
  if let meters = self.height?.quantity.doubleValueForUnit(HKUnit.meterUnit()) {
    let heightFormatter = NSLengthFormatter()
    heightFormatter.forPersonHeightUse = true;
    heightLocalizedString = heightFormatter.stringFromMeters(meters);
  }


  // 4. Update UI. HealthKit use an internal queue. We make sure that we interact with the UI in the main thread
  dispatch_async(dispatch_get_main_queue(), { () -> Void in
    self.heightLabel.text = heightLocalizedString
  });
})

}

As you notice I'm creating an HKSampleType constant then pass it to a method called readMostRecentSample which takes this parameter and then returns the most recent record for this sample type.

I tried to print line the returned object and I've got this output:

1.9 m "Health" metadata: { HKWasUserEntered = 1; } 2015-05-17 10:11:00 +0300 2015-05-17 10:11:00 +0300

As you see the output includes the metadata of the object, but actually I couldn't extract only the date.

Also I found that there is a property of the object called metadata, I printed it out but it only retrieved me a boolean of whether the data was entered by the user (manually) or automatically from a third party: println(self.height?.metadata)

The output was: [HKWasUserEntered = 1]

I would be grateful and thankful if someone can give me any idea of how to extract the metadata of each object.


Solution

  • A HKSample object and its subclasses like HKQuantitySample have 2 fields that store date information : startDate and endDate. If you are trying to get the date this is where you should look.

    Some samples—for example, body temperature—represent a single point in time. For these samples, both the start and the end date are the same, because they both refer to the point in time when the sample was taken.

    Other samples—for example, step count—represent data over a time interval. Here, the sample should use different start and end dates. These dates mark the beginning and end of the sample’s time interval, respectively.

    From the documentation https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKSample_Class/index.html#//apple_ref/occ/instp/HKSample/startDate