繁体   English   中英

如何从HealthKit获取元数据?

[英]How to get metadata from HealthKit?

我正在开发一个从HealthKit应用程序读取不同健康数据的应用程序。

到目前为止,我设法获得了DOB,包括身高,体重和血糖的最新记录。

我仍然需要如何获取这些对象的元数据,特别是我需要获取输入记录的日期/时间。

例如,要获取高度记录,我正在使用以下方法:

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
  });
})

}

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

我尝试打印返回对象的行,并得到以下输出:

1.9 m“健康”元数据:{HKWasUserEntered = 1; } 2015-05-17 10:11:00 +0300 2015-05-17 10:11:00 +0300

如您所见,输出包括对象的元数据,但实际上我无法仅提取日期。

我还发现对象有一个属性,称为metadata ,我将其打印出来,但它仅检索了一个布尔值,即数据是由用户(手动)输入还是从第三方自动输入的: println(self.height?.metadata)

输出为: [HKWasUserEntered = 1]

如果有人可以给我关于如何提取每个对象的元数据的任何想法,我将不胜感激。

HKSample对象及其子类(例如HKQuantitySample具有2个用于存储日期信息的字段: startDateenDate 如果您想获取日期,则应在此处查找。

一些样本(例如,体温)代表一个时间点。 对于这些样本,开始日期和结束日期都是相同的,因为它们都指取样的时间点。

其他样本(例如,步数)表示一个时间间隔内的数据。 在这里,样本应使用不同的开始日期和结束日期。 这些日期分别标记了样本时间间隔的开始和结束。

从文档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