簡體   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