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

Getting step count on iOS

Getting step count on iOS

iOSで正しく歩数を取得する方法を説明します。

sato-shin

March 27, 2019
Tweet

More Decks by sato-shin

Other Decks in Programming

Transcript

  1. let store = HKHealthStore() let type = HKSampleType.quantityType(forIdentifier: .stepCount)! let

    predicate = HKQuery.predicateForSamples(withStart: start, end: end) let query = HKSampleQuery(sampleType: type, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, result, error) in let result = result as! [HKQuantitySample] let sum = result(0) { $0 + $1.quantity.doubleValue(for: .count()) } print(sum) } store.execute(query) )FBMUI,JUΛར༻͢Δ
  2. let store = HKHealthStore() let type = HKSampleType.quantityType(forIdentifier: .stepCount)! let

    predicate = HKQuery.predicateForSamples(withStart: start, end: end) let query = HKSampleQuery(sampleType: type, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, result, error) in let result = result as! [HKQuantitySample] let sum = result(0) { $0 + $1.quantity.doubleValue(for: .count()) } print(sum) } store.execute(query) )FBMUI,JUΛར༻͢Δ า਺Λ ظؒͰ શͯऔಘ ΫΤϦ࣮ߦ DPNQMFUJPOIBOEMFS
  3. let store = HKHealthStore() let type = HKSampleType.quantityType(forIdentifier: .stepCount)! let

    predicate = HKQuery.predicateForSamples(withStart: start, end: end) let query = HKSampleQuery(sampleType: type, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, result, error) in let result = result as! [HKQuantitySample] let sum = result(0) { $0 + $1.quantity.doubleValue(for: .count()) } print(sum) } store.execute(query) )FBMUI,JUΛར༻͢Δ
  4. let store = HKHealthStore() let type = HKSampleType.quantityType(forIdentifier: .stepCount)! let

    predicate = HKQuery.predicateForSamples(withStart: start, end: end) let query = HKSampleQuery(sampleType: type, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, result, error) in let result = result as! [HKQuantitySample] let sum = result(0) { $0 + $1.quantity.doubleValue(for: .count()) } print(sum) } store.execute(query) )FBMUI,JUΛར༻͢Δ
  5. let store = HKHealthStore() let type = HKSampleType.quantityType(forIdentifier: .stepCount)! let

    predicate = HKQuery.predicateForSamples(withStart: start, end: end) let query = HKSampleQuery(sampleType: type, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, result, error) in let result = result as! [HKQuantitySample] let sum = result(0) { $0 + $1.quantity.doubleValue(for: .count()) } print(sum) } store.execute(query) )FBMUI,JUΛར༻͢Δ HKSampleQuery ɹ৚݅ʹϚον͢Δ ɹݸʑͷ஋Λऔಘ͢Δ
  6. let store = HKHealthStore() let type = HKSampleType.quantityType(forIdentifier: .stepCount)! let

    predicate = HKQuery.predicateForSamples(withStart: start, end: end) let query = HKSampleQuery(sampleType: type, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, result, error) in let result = result as! [HKQuantitySample] let sum = result(0) { $0 + $1.quantity.doubleValue(for: .count()) } print(sum) } store.execute(query) )FBMUI,JUΛར༻͢Δ HKSampleQuery ɹ৚݅ʹϚον͢Δ ɹݸʑͷ஋Λऔಘ͢Δ ɹݸʑͷ஋Ͱ͸ͳ͘ɺ ɹσʔλͷಛ௃͕஌Γ͍ͨ
  7. let store = HKHealthStore() let type = HKSampleType.quantityType(forIdentifier: .stepCount)! let

    predicate = HKQuery.predicateForSamples(withStart: start, end: end) let query = HKStatisticsQuery(quantityType: type, quantitySamplePredicate: predicate, options: .cumulativeSum) { (query, statistics, error) in print(statistics?.sumQuantity()) } store.execute(query) )FBMUI,JUΛར༻͢Δ ྦྷੵ࿨ ॏෳʹର͍͍ͯ͠ײ͡ʹܭࢉͯ͘͠ΕΔ☺