Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introducing Cardio

3,021 views

Published on

Simple HealthKit wrapper library

Published in: Technology
  • Be the first to comment

Introducing Cardio

  1. 1. INTRODUCING CARDIOMERCARI INC. @KITASUKE
  2. 2. ABOUT ME
  3. 3. MY FUTURE TALKS > Search APIs and Universal Links in practice @iOS9 bootcamp > Introducing new features for watchOS 2 @MOSA software meeting
  4. 4. HEALTHKIT HealthKit allows apps that provide health and fitness services — HealthKit Framework Reference
  5. 5. CLASSES > HKDevice > HKSample > HKObjectType > HKQuantity > HKUnit > HKQuery
  6. 6. HKQUERY > HKSampleQuery > HKObserverQuery > HKAnchoredObjectQuery > HKStatisticsQuery > HKStatisticsCollectionQuery > HKCorrelation > HKSourceQuery
  7. 7. HEART RATE IN WATCHOS-2-SAMPLER class HeartRateInterfaceController: WKInterfaceController { let healthStore = HKHealthStore() let heartRateType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)! let heartRateUnit = HKUnit(fromString: "count/min") var heartRateQuery: HKQuery? override func willActivate() { super.willActivate() guard HKHealthStore.isHealthDataAvailable() else { return } let dataTypes = Set([heartRateType]) healthStore.requestAuthorizationToShareTypes(nil, readTypes: dataTypes) { (success, error) -> Void in guard success else { return } } } @IBAction func fetchBtnTapped() { guard self.heartRateQuery == nil else { return } if self.heartRateQuery == nil { self.heartRateQuery = self.createStreamingQuery() self.healthStore.executeQuery(self.heartRateQuery!) } else { self.healthStore.stopQuery(self.heartRateQuery!) self.heartRateQuery = nil } } private func createStreamingQuery() -> HKQuery { let predicate = HKQuery.predicateForSamplesWithStartDate(NSDate(), endDate: nil, options: .None) let query = HKAnchoredObjectQuery(type: heartRateType, predicate: predicate, anchor: nil, limit: Int(HKObjectQueryNoLimit)) { (query, samples, deletedObjects, anchor, error) -> Void in self.addSamples(samples) } query.updateHandler = { (query, samples, deletedObjects, anchor, error) -> Void in self.addSamples(samples) } return query } private func addSamples(samples: [HKSample]?) { guard let samples = samples as? [HKQuantitySample] else { return } guard let quantity = samples.last?.quantity else { return } let heartRate = quantity.doubleValueForUnit(heartRateUnit) } }
  8. 8. !
  9. 9. CARDIO!HTTPS://GITHUB.COM/KITASUKE/CARDIO
  10. 10. HEALTHKIT WRAPPER SPECIALIZED FOR WORKOUT
  11. 11. FEATURES > Super simple to access HealthKit > Cuztomizable preferences > Well-designed for workout app > Easy way to save workout data in HealthKit
  12. 12. REQUIREMENTS > watchOS 2.0+ > Swift 2.0+
  13. 13. INSTALLATION Carthage github "kitasuke/Cardio" Add HealthKit entitlements into both containing iOS app target and WatchKit Extension target
  14. 14. CONTEXT PROTOCOL public protocol Context { var activityType: HKWorkoutActivityType { get } var locationType: HKWorkoutSessionLocationType { get } var distanceUnit: HKUnit { get } var activeEnergyUnit: HKUnit { get } var heartRateUnit: HKUnit { get } var distanceType: HKQuantityType { get } var activeEnergyType: HKQuantityType { get } var heartRateType: HKQuantityType { get } var shareIdentifiers: [String] { get } var readIdentifiers: [String] { get } }
  15. 15. CONTEXT EXTENSION public extension Context { public var activityType: HKWorkoutActivityType { return .Running } public var locationType: HKWorkoutSessionLocationType { return .Outdoor } public var distanceUnit: HKUnit { return HKUnit.meterUnitWithMetricPrefix(.Kilo) } public var activeEnergyUnit: HKUnit { return HKUnit.kilocalorieUnit() } public var heartRateUnit: HKUnit { return HKUnit(fromString: "count/min") } ... }
  16. 16. STRUCT CONFORMED CONTEXT struct CardioContext: Context { var locationType: HKWorkoutSessionLocationType { return .Indoor } var distanceUnit: HKUnit { return HKUnit.mileUnit() } var shareIdentifiers: [String] { return [] } }
  17. 17. INITIALIZATION let context = CardioContext() cardio = Cardio(context: context)
  18. 18. AUTHORIZATION WATCHKIT EXTENSION TARGET cardio.authorize { result in } CONTAINING IOS APP TARGET func applicationShouldRequestHealthAuthorization(application: UIApplication) { authorize() }
  19. 19. UPDATE HANDLERS cardio.distanceHandler = { distance, total in } cardio.activeEnergyHandler = { activeEnergy, total in } cardio.heartRateHandler = { heartRate, average in }
  20. 20. WORKOUT SESSION cardio.start { result in } cardio.end { result in }
  21. 21. SAVE WORKOUT DATA cardio.save() { result in }
  22. 22. FUTURE WORK > Add more features > Fully support HealthKit > Make wrapper for iOS app target
  23. 23. WELCOME YOUR FEEDBACK & THOUGHTS !
  24. 24. ANY QUESTIONS?

×