【发布时间】:2015-05-28 09:52:56
【问题描述】:
我正在开发一个从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 常量,然后将其传递给一个名为 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]
如果有人能告诉我如何提取每个对象的元数据,我将不胜感激。
【问题讨论】: