我今天正在使用以下代碼從healthkit獲取步驟。獲取今天的所有步驟,但從Health kit中使用swift截取手動添加的步驟
func retrieveStepCount(completion: (stepRetrieved: Double) -> Void) {
let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
let date = NSDate()
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let newDate = cal.startOfDayForDate(NSDate())
let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None) // Our search predicate which will fetch all steps taken today
let interval: NSDateComponents = NSDateComponents()
interval.day = 1
let query = HKStatisticsCollectionQuery(quantityType: type!, quantitySamplePredicate: predicate, options: .CumulativeSum, anchorDate: newDate as NSDate, intervalComponents:interval as NSDateComponents)
query.initialResultsHandler = { query, results, error in
if error != nil {
print("Something went Wrong")
return
}
if let myResults = results{
myResults.enumerateStatisticsFromDate(newDate, toDate: NSDate()) {
statistics, stop in
if let quantity = statistics.sumQuantityForSource(HKSource.defaultSource()) {
let steps = quantity.doubleValueForUnit(HKUnit.countUnit())
print("Steps = \(Int(steps))")
completion(stepRetrieved: steps)
}
}
}
}
executeQuery(query)
}
現在可以說,我在總
看,我這是由設備自動檢測到了一些措施,這些措施。還有一些是通過其他一些應用程序添加到heathkit。
我希望他們與我m如果他們兩個,但問題是當用戶的一些一些手冊冊步驟來healthkit。
我不想讓這些手動添加步驟。所以基本上我想得到(5,793 - 2300)= 3493步。
我該怎麼做?我試圖獲取HKSource的名稱我知道當用戶手動輸入步驟時,源的名稱是「健康」,但是如何在此基礎上篩選步驟?請指導我這個,我在這裏想念什麼?在此先感謝
ohh我不知道這一點。謝謝 !! – Byte