-Code finden, wie Trainings-Sitzung starten oder zu stoppen.
let healthStore = HKHealthStore()
healthStore.startWorkoutSession(workoutSession) {
(result: Bool, error: NSError?) -> Void in
}
healthStore.stopWorkoutSession(workoutSession) {
(result: Bool, error: NSError?) -> Void in
}
Es gibt HKWoorkoutSessionDelegate, das seinen Sitzungsstatus benachrichtigt.
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;
Achtung: Es gibt eine Änderung des Methodennamen mit der neuesten Version, Watch OS 2 beta 5.
stopWorkoutSession wurde in endWorkoutSession geändert.
Um genauer zu sein, wenn ich meine Uhr kippen, Bildschirm wird ausgeschaltet, Programm wird gehängt. Wenn ich nach hinten kippe, schaltet sich der Bildschirm ein, die HKworkout-Sitzung wechselt zu diesem Zeitpunkt zuerst auf ".Ended" und dann auf ".Running". Wie man dies verhindert und die Sitzung immer ".Running" – user3781258
Klingt wie ein HealthKit-Fehler, den Sie Apple melden sollten. – Allan
@ user3781258 Irgendwelche Fortschritte in dieser Angelegenheit? – Philip