简体   繁体   English

如何从HealthKit获取元数据?

[英]How to get metadata from HealthKit?

I'm working on an application that reads different health data from HealthKit application. 我正在开发一个从HealthKit应用程序读取不同健康数据的应用程序。

So far I managed to get the DOB, most recent records of height, weight and blood glucose. 到目前为止,我设法获得了DOB,包括身高,体重和血糖的最新记录。

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. 如您HKSampleType我正在创建一个HKSampleType常量,然后将其传递到一个名为readMostRecentSample的方法,该方法使用此参数,然后返回此样本类型的最新记录。

I tried to print line the returned object and I've got this output: 我尝试打印返回对象的行,并得到以下输出:

1.9 m "Health" metadata: { HKWasUserEntered = 1; 1.9 m“健康”元数据:{HKWasUserEntered = 1; } 2015-05-17 10:11:00 +0300 2015-05-17 10:11:00 +0300 } 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) 我还发现对象有一个属性,称为metadata ,我将其打印出来,但它仅检索了一个布尔值,即数据是由用户(手动)输入还是从第三方自动输入的: println(self.height?.metadata)

The output was: [HKWasUserEntered = 1] 输出为: [HKWasUserEntered = 1]

I would be grateful and thankful if someone can give me any idea of how to extract the metadata of each object. 如果有人可以给我关于如何提取每个对象的元数据的任何想法,我将不胜感激。

A HKSample object and its subclasses like HKQuantitySample have 2 fields that store date information : startDate and enDate . HKSample对象及其子类(例如HKQuantitySample具有2个用于存储日期信息的字段: startDateenDate 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 从文档https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKSample_Class/index.html#//apple_ref/occ/instp/HKSample/startDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM