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.
Howto make
workoutapp
for watchOS 2
Mercari Inc.
@kitasuke
HealthKit
“Apps can useto
share healthand
fitness data”
HealthKit Framework Reference
Sample Objects
» Characteristics
» Samples
» Category Samples
» Quantity Samples
» Correlations
» Workouts
Main flowforworkoutapps
» Start workout session
» Query data in HealthKit
» Stop workout session
» Save data in HealthKit
What'sWorkoutSession
» Workout sessions allow apps to run in the
foreground
» Apple Watch can only run one workout session...
WorkoutSession
class HKWorkoutSession: NSObject {
var activityType: HKWorkoutActivityType { get }
var locationType: HKWork...
WorkoutActivityType
enum HKWorkoutActivityType : UInt {
case Basketball
case Fishing
case Gymnastics
case Hunting
case Mar...
WorkoutLocationType
enum HKWorkoutSessionLocationType : Int {
case Unknown
case Indoor
case Outdoor
}
WorkoutSession delegate
protocol HKWorkoutSessionDelegate : NSObjectProtocol {
func workoutSession(workoutSession: HKWorko...
Startingand StoppingWorkout
Sessions
healthStore.startWorkoutSession(workoutSession) {
(result: Bool, error: NSError?) -> ...
Setupfor HealthKit
1.Enable capabilities in Xcode
2.Check availability
3.Instantiate an HKHealthStore object
4.Request aut...
WatchKit Extension
let healthStore = HKHealthStore()
if HKHealthStore.isHealthDataAvailable() {
let energy = HKQuantityTyp...
Containing iOS app
func applicationShouldRequestHealthAuthorization(application: UIApplication) {
HKHealthStore().handleAu...
Accessing HealthKitData
» Direct method calls
» Sample query
» Observer query
» Anchored object query
» Statistics query
»...
Streaming Updates
let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: nil, options: .None)
let ty...
Savingworkout
let workout = HKWorkout(activityType: activityType,
startDate: startDate,
endDate: endDate,
duration: endDat...
Howto Debug
Better debug on device, not simulator
Because
- Setting location to City Run or City Bicycle Ride
doesn't work...
Known Issues?
Can notattachto process on
device
“Did the 'trust this computer' diaglog ever show on
the watch? Unpairing and then re-pai...
WorkoutSession doesn'tkeepin
the foreground
Might be a bug?
HKWorkoutSession and keeping the app in the
foreground
Having ...
InfrequentUpdates
updateHandler is not stable?
HKWorkoutSession with HKAnchoredObjectQuery has very
infrequent updates
Batteryuse
Not particularly workout app issue, but should be
well-considered
Battery Use Worse
Summary
» Can access to a bunch of fitness information
» Customizable for specific use
» So much pain to debug
Thankyou
How to make workout app for watch os 2
How to make workout app for watch os 2
How to make workout app for watch os 2
How to make workout app for watch os 2
How to make workout app for watch os 2
How to make workout app for watch os 2
How to make workout app for watch os 2
How to make workout app for watch os 2
Upcoming SlideShare
Loading in …5
×

How to make workout app for watch os 2

13,751 views

Published on

WWDC Afterparty Roppongi

Published in: Engineering

How to make workout app for watch os 2

  1. 1. Howto make workoutapp for watchOS 2 Mercari Inc. @kitasuke
  2. 2. HealthKit
  3. 3. “Apps can useto share healthand fitness data” HealthKit Framework Reference
  4. 4. Sample Objects » Characteristics » Samples » Category Samples » Quantity Samples » Correlations » Workouts
  5. 5. Main flowforworkoutapps » Start workout session » Query data in HealthKit » Stop workout session » Save data in HealthKit
  6. 6. What'sWorkoutSession » Workout sessions allow apps to run in the foreground » Apple Watch can only run one workout session at a time
  7. 7. WorkoutSession class HKWorkoutSession: NSObject { var activityType: HKWorkoutActivityType { get } var locationType: HKWorkoutSessionLocationType { get } weak var delegate: HKWorkoutSessionDelegate? var state: HKWorkoutSessionState { get } init(activityType: HKWorkoutActivityType, locationType: HKWorkoutSessionLocationType) }
  8. 8. WorkoutActivityType enum HKWorkoutActivityType : UInt { case Basketball case Fishing case Gymnastics case Hunting case MartialArts case Running case Soccer case Swimming case Walking case Yoga … case Other }
  9. 9. WorkoutLocationType enum HKWorkoutSessionLocationType : Int { case Unknown case Indoor case Outdoor }
  10. 10. WorkoutSession delegate protocol HKWorkoutSessionDelegate : NSObjectProtocol { func workoutSession(workoutSession: HKWorkoutSession, didChangeToState toState: HKWorkoutSessionState, fromState: HKWorkoutSessionState, date: NSDate) func workoutSession(workoutSession: HKWorkoutSession, didFailWithError error: NSError) }
  11. 11. Startingand StoppingWorkout Sessions healthStore.startWorkoutSession(workoutSession) { (result: Bool, error: NSError?) -> Void in } healthStore.stopWorkoutSession(workoutSession) { (result: Bool, error: NSError?) -> Void in }
  12. 12. Setupfor HealthKit 1.Enable capabilities in Xcode 2.Check availability 3.Instantiate an HKHealthStore object 4.Request authorization
  13. 13. WatchKit Extension let healthStore = HKHealthStore() if HKHealthStore.isHealthDataAvailable() { let energy = HKQuantityTypeIdentifierActiveEnergyBurned)! let walkingRunning = HKQuantityTypeIdentifierDistanceWalkingRunning)! let heartRate = HKQuantityTypeIdentifierHeartRate)! let cycling = HKQuantityTypeIdentifierDistanceCycling)! let typesToShare = Set([HKWorkoutType.workoutType()]) let typesToRead = Set([ HKObjectType.quantityTypeForIdentifier(energy, HKObjectType.quantityTypeForIdentifier(walkingRunning, HKObjectType.quantityTypeForIdentifier(heartRate, HKObjectType.quantityTypeForIdentifier(cycling ]) healthStore.requestAuthorizationToShareTypes(typesToShare, readTypes: typesToRead, completion: { (result: Bool, error: NSError?) -> Void in }) }
  14. 14. Containing iOS app func applicationShouldRequestHealthAuthorization(application: UIApplication) { HKHealthStore().handleAuthorizationForExtensionWithCompletion { (result: Bool, error: NSError?) -> Void in } }
  15. 15. Accessing HealthKitData » Direct method calls » Sample query » Observer query » Anchored object query » Statistics query » Statistics collection query » Correlation query » Source query
  16. 16. Streaming Updates let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: nil, options: .None) let type = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceCycling)! let distanceQuery = HKAnchoredObjectQuery(type: type, predicate: predicate, anchor: 0, limit: 0) { (query, samples, deletedObjects, anchor, error) -> Void in } distanceQuery.updateHandler = { (query, samples, deletedObjects, anchor, error) -> Void in } healthStore.executeQuery(distanceQuery)
  17. 17. Savingworkout let workout = HKWorkout(activityType: activityType, startDate: startDate, endDate: endDate, duration: endDate.timeIntervalSinceDate(startDate), totalEnergyBurned: totalEnergyBurned, totalDistance: totalDistance, metadata: nil) healthStore.saveObject(workout) { (result: Bool, error: NSError?) -> Void in }
  18. 18. Howto Debug Better debug on device, not simulator Because - Setting location to City Run or City Bicycle Ride doesn't work for HealthKit - Need to save data in HealthKit manually - Big behavior differences between device and simulator
  19. 19. Known Issues?
  20. 20. Can notattachto process on device “Did the 'trust this computer' diaglog ever show on the watch? Unpairing and then re-pairing and restarting Xcode should have gotten you that prompt. The symptoms you are experiencing seem to indicate that this dialog was not presented after you upgraded.” On device watchOS debugging impossible
  21. 21. WorkoutSession doesn'tkeepin the foreground Might be a bug? HKWorkoutSession and keeping the app in the foreground Having a workout count toward green exercise ring
  22. 22. InfrequentUpdates updateHandler is not stable? HKWorkoutSession with HKAnchoredObjectQuery has very infrequent updates
  23. 23. Batteryuse Not particularly workout app issue, but should be well-considered Battery Use Worse
  24. 24. Summary » Can access to a bunch of fitness information » Customizable for specific use » So much pain to debug
  25. 25. Thankyou

×