+ All Categories
Home > Engineering > How to make workout app for watch os 2

How to make workout app for watch os 2

Date post: 05-Aug-2015
Category:
Upload: yusuke-kita
View: 1,934 times
Download: 5 times
Share this document with a friend
33
How to make workout app for watchOS 2 Mercari Inc. @kitasuke
Transcript
Page 1: How to make workout app for watch os 2

How to makeworkout appfor watchOS 2

Mercari Inc.@kitasuke

Page 2: How to make workout app for watch os 2
Page 3: How to make workout app for watch os 2
Page 4: How to make workout app for watch os 2
Page 5: How to make workout app for watch os 2
Page 6: How to make workout app for watch os 2
Page 7: How to make workout app for watch os 2
Page 8: How to make workout app for watch os 2

HealthKit

Page 10: How to make workout app for watch os 2

Sample Objects» Characteristics

» Samples

» Category Samples

» Quantity Samples

» Correlations

» Workouts

Page 11: How to make workout app for watch os 2

Main flow for workout apps» Start workout session

» Query data in HealthKit

» Stop workout session

» Save data in HealthKit

Page 12: How to make workout app for watch os 2

What's Workout Session» Workout sessions allow apps to run in the

foreground

» Apple Watch can only run one workout session at a time

Page 13: How to make workout app for watch os 2

Workout Sessionclass HKWorkoutSession: NSObject {

var activityType: HKWorkoutActivityType { get } var locationType: HKWorkoutSessionLocationType { get } weak var delegate: HKWorkoutSessionDelegate? var state: HKWorkoutSessionState { get }

init(activityType: HKWorkoutActivityType, locationType: HKWorkoutSessionLocationType)}

Page 14: How to make workout app for watch os 2

Workout Activity Typeenum HKWorkoutActivityType : UInt {

case Basketball case Fishing case Gymnastics case Hunting case MartialArts case Running case Soccer case Swimming case Walking case Yoga …

case Other}

Page 15: How to make workout app for watch os 2

Workout Location Typeenum HKWorkoutSessionLocationType : Int {

case Unknown case Indoor case Outdoor}

Page 16: How to make workout app for watch os 2

Workout Session delegateprotocol HKWorkoutSessionDelegate : NSObjectProtocol {

func workoutSession(workoutSession: HKWorkoutSession, didChangeToState toState: HKWorkoutSessionState, fromState: HKWorkoutSessionState, date: NSDate)

func workoutSession(workoutSession: HKWorkoutSession, didFailWithError error: NSError)}

Page 17: How to make workout app for watch os 2

Starting and Stopping Workout SessionshealthStore.startWorkoutSession(workoutSession) { (result: Bool, error: NSError?) -> Void in}

healthStore.stopWorkoutSession(workoutSession) { (result: Bool, error: NSError?) -> Void in}

Page 18: How to make workout app for watch os 2

Setup for HealthKit1.Enable capabilities in Xcode

2.Check availability

3.Instantiate an HKHealthStore object

4.Request authorization

Page 19: How to make workout app for watch os 2

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 })}

Page 20: How to make workout app for watch os 2

Containing iOS app

func applicationShouldRequestHealthAuthorization(application: UIApplication) { HKHealthStore().handleAuthorizationForExtensionWithCompletion { (result: Bool, error: NSError?) -> Void in }}

Page 21: How to make workout app for watch os 2
Page 22: How to make workout app for watch os 2
Page 23: How to make workout app for watch os 2

Accessing HealthKit Data» Direct method calls

» Sample query

» Observer query

» Anchored object query

» Statistics query

» Statistics collection query

» Correlation query

» Source query

Page 24: How to make workout app for watch os 2

Streaming Updateslet 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)

Page 25: How to make workout app for watch os 2

Saving workoutlet 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}

Page 26: How to make workout app for watch os 2

How to DebugBetter 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

Page 27: How to make workout app for watch os 2

Known Issues?

Page 28: How to make workout app for watch os 2

Can not attach to 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

Page 29: How to make workout app for watch os 2

Workout Session doesn't keep in the foregroundMight be a bug?

HKWorkoutSession and keeping the app in the foregroundHaving a workout count toward green exercise ring

Page 30: How to make workout app for watch os 2

Infrequent UpdatesupdateHandler is not stable?

HKWorkoutSession with HKAnchoredObjectQuery has very infrequent updates

Page 31: How to make workout app for watch os 2

Battery useNot particularly workout app issue, but should be well-considered

Battery Use Worse

Page 32: How to make workout app for watch os 2

Summary» Can access to a bunch of fitness information

» Customizable for specific use

» So much pain to debug

Page 33: How to make workout app for watch os 2

Thank you


Recommended