Detect "screenshot" press while in background on Apple Watch

112 Views Asked by At

I have followed this answer to make my app detect when the crown and side button are pressed at the same time. This works for me, but only when the app is in the foreground. My app uses an HKWorkoutSession to stay running in the background, but it does not detect the press of the two buttons. I need the app to detect the pressing of both buttons in the background for my app to work properly.

My code for setting up the workout session is:

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) ***")
    }
}

The code I have so far to detect the buttons being pressed is:

func workoutSession(_ workoutSession: HKWorkoutSession, didGenerate   event: HKWorkoutEvent) {
    if(event.type == HKWorkoutEventType.pauseOrResumeRequest) {
        print("Detected Press")
    }
}

Thanks!

0

There are 0 best solutions below