我正在开发Apple watch app,我正在使用HKworkoutsession来访问心率数据样本。
在最新的watchos2 beta3发布错误中“在活动的锻炼期间,屏幕关闭时不会生成新的心率样本。”是固定的。
我的问题是我怎样才能将我的HKworkoutsession永远设置为“积极的锻炼课程”,因此我可以根据需要随时获取心率样本。
由于 莱恩
答案 0 :(得分:5)
以下代码是如何开始或停止锻炼课程。
let healthStore = HKHealthStore()
healthStore.startWorkoutSession(workoutSession) {
(result: Bool, error: NSError?) -> Void in
}
healthStore.stopWorkoutSession(workoutSession) {
(result: Bool, error: NSError?) -> Void in
}
有HKWorkoutSessionDelegate通知其会话状态。
protocol HKWorkoutSessionDelegate : NSObjectProtocol {
func workoutSession(workoutSession: HKWorkoutSession,
didChangeToState toState: HKWorkoutSessionState,
fromState: HKWorkoutSessionState, date: NSDate)
func workoutSession(workoutSession: HKWorkoutSession,
didFailWithError error: NSError)
}
[编辑] 2015/08/31
ObjC版
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;
警告:使用最新版本更改方法名称,观看OS 2 beta 5。
stopWorkoutSession已更改为endWorkoutSession。