我遵循了此answer,使我的应用能够检测到何时同时按下表冠和侧面按钮。这对我有效,但仅在应用程序处于前台时才有效。我的应用程序使用HKWorkoutSession来在后台运行,但未检测到两个按钮的按下。我需要该应用程序检测背景中两个按钮的按下状态,以使我的应用程序正常工作。
我用于设置锻炼课程的代码是:
func configureWorkout() {
configuration.activityType = .walking
configuration.locationType = .unknown
do {
session = try HKWorkoutSession(healthStore: store, configuration: configuration)
session!.delegate = self
session!.startActivity(with: dateManager.getDate())
}
catch let error as NSError {
fatalError("*** Unable to create the workout session: \(error.localizedDescription) ***")
}
}
到目前为止,我能检测到按下按钮的代码是:
func workoutSession(_ workoutSession: HKWorkoutSession, didGenerate event: HKWorkoutEvent) {
if(event.type == HKWorkoutEventType.pauseOrResumeRequest) {
print("Detected Press")
}
}
谢谢!