You need to create a HKworkoutSession and collect the motion data when the session is active. The session allows you to collect motion data even in the background i.e., when the screen is turned off.
Use the below given link to know more about creating a workout session
(https://developer.apple.com/documentation/healthkit/workouts_and_activity_rings/build_a_workout_app_for_apple_watch).
Topic:
Programming Languages
SubTopic:
Swift
Tags: