I'm working on apple watch app and I'm using HKworkoutsession to access heart rate data sample.
In the newest watchos2 beta3 release bug "During an active workout session, new heart rate samples are not generated when the screen is off." is fixed.
My question is how could I set my HKworkoutsession always as an "active work out session" thus I could keep getting heart rate sample as I need.
Thanks Ryan
WorkoutSession is used to store data related to the current PrancerciseWorkout being tracked. Since you added in this concept of workout intervals to PrancerciseWorkout, WorkoutSession needs to add new intervals each time you start and end a Prancercise session.
The first thing you do is create a health store and workout configuration to store data into HealthKit. Next, add the following to the end of the method: Use the HKWorkoutBuilder class (new to iOS 12) to incrementally construct a workout and begin collecting workout data.
A workout is a container for these types of information, taken as a collection of samples. A given workout might contain heart rate samples, distance samples and an activity type to categorize them. Continuing from the previous HealthKit tutorial, you’re going to track a special kind of workout: Prancercise.
HKQuery.predicateForWorkouts (with:) is a special method that gives you a predicate for workouts with a certain activity type. In this case, you’re loading any type of workout in which the activity type is other (all Prancercise workouts use the other activity type).
Code following is how to start or stop workout session.
let healthStore = HKHealthStore()
healthStore.startWorkoutSession(workoutSession) {
(result: Bool, error: NSError?) -> Void in
}
healthStore.stopWorkoutSession(workoutSession) {
(result: Bool, error: NSError?) -> Void in
}
There is HKWorkoutSessionDelegate which notifies its session state.
protocol HKWorkoutSessionDelegate : NSObjectProtocol {
func workoutSession(workoutSession: HKWorkoutSession,
didChangeToState toState: HKWorkoutSessionState,
fromState: HKWorkoutSessionState, date: NSDate)
func workoutSession(workoutSession: HKWorkoutSession,
didFailWithError error: NSError)
}
[Edited] 2015/08/31
ObjC version
HKWorkoutSession *workoutSession = [[HKWorkoutSession alloc] initWithActivityType:HKWorkoutActivityTypeRunning locationType:HKWorkoutSessionLocationTypeOutdoor];
workoutSession.delegate = self;
HKHealthStore *healthStore = [HKHealthStore new];
[healthStore startWorkoutSession:workoutSession];
[healthStore stopWorkoutSession:workoutSession];
HKWrokoutSessionDelegate
- (void)workoutSession:(HKWorkoutSession *)workoutSession
didChangeToState:(HKWorkoutSessionState)toState
fromState:(HKWorkoutSessionState)fromState
date:(NSDate *)date;
- (void)workoutSession:(HKWorkoutSession *)workoutSession didFailWithError:(NSError *)error;
Caution: There is a change of method name with the latest version, watch OS 2 beta 5.
stopWorkoutSession has changed to endWorkoutSession.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With