Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iPhoneで正しい1日の歩数を取得する方法

 iPhoneで正しい1日の歩数を取得する方法

yamada-ysys

April 12, 2019
Tweet

Other Decks in Programming

Transcript

  1. 1೔෼ͷา਺ͷऔಘ // ݕࡧର৅ let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)! // ݕࡧ৚݅

    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [.strictStartDate]) let query = HKStatisticsQuery(quantityType: type, quantitySamplePredicate: predicate, options: [.cumulativeSum, .separateBySource], completionHandler: { (query, result, error) in var steps = 0 if let doubleSteps = result?.sumQuantity()?.doubleValue(for: HKUnit.count()){ steps = Int(doubleSteps) } }) healthStore.execute(query)
  2. ਖ਼͍͠1೔෼ͷา਺ͷऔಘ // ݕࡧର৅ let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)! // ݕࡧ৚݅

    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: []) let query = HKStatisticsCollectionQuery(quantityType: type, quantitySamplePredicate: predicate, options: [.cumulativeSum, .separateBySource], anchorDate: anchorDate, intervalComponents: intervalComponents) query.initialResultsHandler = { [unowned self] (query, result, error) in guard let result = result, error == nil else { return } for item in result.statistics() { print("\(item.startDate) - \(item.endDate) \(item.sumQuantity()!.doubleValue(for: HKUnit.count()))”) } } healthStore.execute(query)