简体   繁体   中英

Retrieve HKWorkoutTypeIdentifier from a HealthKit query

In a HealthKit query, is there a way to retrieve the original HKWorkoutTypeIdentifier used to store the data?

I know you can use HKWorkoutTypeIdentifier to query for specific workout types; but, that's not what I'm trying to do. The question I want to answer, is "What types of workouts has the user done?"

The Apple Health app has a list, as do some third-party apps. But I can't figure out how they're doing this.

Any ideas?

Once you've got the samples returned from the query, you can use .workoutActivityType on each workout sample to access the HKWorkoutActivityType that's set for that sample.

Just as Belle said, you can use.workoutActivityType on a sample. However that only returns the HKWorkoutActivityType, which is a Uinit. If you want to display it as a string, for example "running" you need to use the extension mentioned here .

Then you can use.name to return a string with the activity type. So if you have a sample called workout:

let activityType = workout.workoutActivityType.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM