Save 37% off PRO during our Black Friday Sale! »

Getting step count on iOS

Getting step count on iOS

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

028088964f255d677d471558da865b72?s=128

sato-shin

March 27, 2019
Tweet

Transcript

  1. J04Ͱਖ਼͘͠า਺Λऔಘ͢Δ TBUPTIJO!8*5$P-UE

  2. ৯ࣄه࿥ΞϓϦʮ͚͋͢Μʯ ࣌఺ 
 ໿ສਓ͕ར༻͢Δ ৯ࣄه࿥ʹಛԽͨ͠
 lϔϧεέΞzΞϓϦ

  3. ৯ࣄه࿥ΞϓϦʮ͚͋͢Μʯ ࣌఺ 
 ໿ສਓ͕ར༻͢Δ ৯ࣄه࿥ʹಛԽͨ͠
 lϔϧεέΞzΞϓϦ า਺͸ॏཁͳࢦඪͷҰͭ

  4. า਺ʹର͢Δಈػ෇͚ ! ফඅΧϩϦʔͷՄࢹԽ ϙΠϯτ ϙέϞϯ ͓ۚ

  5. J04ʹ͓͚Δา਺ͷऔಘํ๏ w$.1FEPNFUFSΛར༻ w)FBMUI,JUΛར༻

  6. J04ʹ͓͚Δา਺ͷऔಘํ๏ w$.1FEPNFUFSΛར༻ w)FBMUI,JUΛར༻

  7. $.1FEPNFUFSΛར༻͢Δ w TUBSUdFOE·ͰͷؒͰ୺຤Ͱݕ஌ͨ͠า਺Λऔಘ ‣ "QQMF8BUDIͷ৘ใऔಘ͸8BUDI"QQͰ࣮૷ w औಘͰ͖Δͷ͸աڈ೔෼ let pedometer =

    CMPedometer() pedometer.queryPedometerData(from: start, to: end) { (data, error) in print(data?.numberOfSteps) }
  8. J04ʹ͓͚Δา਺ͷऔಘํ๏ .FUIPEPGHFUUJOHTUFQDPVOUPOJ04 w$.1FEPNFUFSΛར༻ w)FBMUI,JUΛར༻

  9. ʮ͍ͭʯɺʮԿาʯ͕֨ೲ )FBMUI,JUΛར༻͢Δ b)FBMUI`BQQา਺͢΂ͯͷσʔλΛදࣔ೔෇

  10. 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Λར༻͢Δ
  11. 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
  12. 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Λར༻͢Δ
  13. J1IPOF͔Βͷσʔλ "QQMF8BUDI͔Βͷσʔλ )FBMUI,JUΛར༻͢Δ ⚠ॏෳͯ͠Χ΢ϯτ͞ΕΔ J1IPOF͚ͩͰςετ͢Δͱɺ ্ख͍ͬͯͦ͘͏ʹݟ͑ΔͷͰ஫ҙ

  14. 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Λར༻͢Δ
  15. 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 ɹ৚݅ʹϚον͢Δ ɹݸʑͷ஋Λऔಘ͢Δ
  16. 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 ɹ৚݅ʹϚον͢Δ ɹݸʑͷ஋Λऔಘ͢Δ ɹݸʑͷ஋Ͱ͸ͳ͘ɺ ɹσʔλͷಛ௃͕஌Γ͍ͨ
  17. ݸʑͷ஋Λऔಘ͍ͨ͠ HKSampleQuery άϧʔϓͷಛ௃Λऔಘ͍ͨ͠(sum,ave,min,max,latest)
 ɹ HKStatisticsQuery / HKStatisticsCollectionQuery )FBMUI,JUΛར༻͢Δ

  18. 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Λར༻͢Δ ྦྷੵ࿨ ॏෳʹର͍͍ͯ͠ײ͡ʹܭࢉͯ͘͠ΕΔ☺
  19. ෆਖ਼ରࡦ )FBMUI,JUΛར༻͢Δ J1IPOFͱ"QQMF8BUDIͷา਺ܭσʔλͷΈΛൈ͖ग़͢ !

  20. )FBMUI,JUΛར༻͢Δ

  21. )FBMUI,JUΛར༻͢Δ खಈͰ௥ՃͰ͖Δ

  22. )FBMUI,JUΛར༻͢Δ )FBMUI"QQDPNBQQMF)FBMUI J1IPOFDPNBQQMFIFBMUI<66*%> ֤߲໨͸ॻ͖ࠐΜͩΞϓϦͷ #VOEMF*EFOUJpFSͷ৘ใΛ࣋ͭ IBT1SFpY lDPNBQQMFIFBMUIz  ͰϑΟϧλʔ͢Ε͹0, "QQMF8BUDIDPNBQQMFIFBMUI<66*%>

  23. 4BNQMFDPEF https://github.com/sato-shin/ios-steps

  24. None