How do I detect the Apple Watch screenshot event/d

2019-06-25 14:16发布

问题:

The native workout app and Sportstracker for example have the ability to pause the workout when a user presses both the digital crown and the side button at the same time, AKA taking a screenshot.

I need something like this but I can't find anything about it in the docs or anywhere else online. For iOS there's the "notificationUIApplicationUserDidTakeScreenshot", but that doesn't work on watchOS.

Anyone know how to do this? Is it publicly available or is this a private API? Did I not look well enough?

回答1:

I haven't tried this, but with some digging around in the HealthKit docs, it looks like:

  1. You presumably have an HKWorkoutSession to run your workout anyway, right? And it'll need an HKWorkoutSessionDelegate handling its state changes and whatnot.

  2. When the user presses the side button and Digital Crown together, your delegate should get a workoutSession(_:didGenerate:) callback, with the corresponding even having an event type of pauseOrResumeRequest. (They might or might not also get a screenshot, but that's out of your control.)

  3. You handle the user's request to pause by calling pause(_:) on the HKHealthStore.