How to make HKworkoutession always an active workout session

I am working on an apple watch application and I am using HKworkoutession to access a sample heart rate data.

In the latest beta version of watchos2 beta3, "During an active workout, new heart rate samples are not generated when the screen is off." fixed.

My question is how can I set HKworkoutession always as an β€œactive work session”, so I could continue to get a heart rate sample as I need.

Thanks Ryan

+5
source share
1 answer

The next code is to start or stop a 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 an HKWorkoutSessionDelegate that notifies 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; 

Note: there is a change in the method name with the latest version, see OS 2 beta 5.

stopWorkoutSession has changed to endWorkoutSession.

+4
source

All Articles