Reputation: 1532
Ok, so the property workoutActivityType
of the HKWorkout
returns a NSInteger
.
Here is a list with the activities: workoutActivityType list
For now, I will create a plist
and look for the activity name when I get the integer
. But it seems a little odd to have to go through this.
Do I need to find out which activity it is on my own or is there something I am missing?
Upvotes: 15
Views: 4367
Reputation: 840
I prefer to use the INT values of the ActTypes so that it's easier to keep track of. (I'm presuming that Apple will add the newest sport at the bottom, incrementing the int value)
These are generated by hand using the definitions of HKWorkoutActivityType (public enum HKWorkoutActivityType : UInt, @unchecked Sendable {)
import HealthKit
extension HKWorkoutActivityType {
static func name(for activityType: HKWorkoutActivityType) -> String {
switch activityType.rawValue {
case 1: return "American Football"
case 2: return "Archery"
case 3: return "Australian Football"
case 4: return "Badminton"
case 5: return "Baseball"
case 6: return "Basketball"
case 7: return "Bowling"
case 8: return "Boxing"
case 9: return "Climbing"
case 10: return "Cricket"
case 11: return "Cross Training"
case 12: return "Curling"
case 13: return "Cycling"
case 14: return "Dance"
case 15: return "Dance Inspired Training"
case 16: return "Elliptical"
case 17: return "Equestrian Sports"
case 18: return "Fencing"
case 19: return "Fishing"
case 20: return "Functional Strength Training"
case 21: return "Golf"
case 22: return "Gymnastics"
case 23: return "Handball"
case 24: return "Hiking"
case 25: return "Hockey"
case 26: return "Hunting"
case 27: return "Lacrosse"
case 28: return "Martial Arts"
case 29: return "Mind and Body"
case 30: return "Mixed Metabolic Cardio Training"
case 31: return "Paddle Sports"
case 32: return "Play"
case 33: return "Preparation and Recovery"
case 34: return "Racquetball"
case 35: return "Rowing"
case 36: return "Rugby"
case 37: return "Running"
case 38: return "Sailing"
case 39: return "Skating Sports"
case 40: return "Snow Sports"
case 41: return "Soccer"
case 42: return "Softball"
case 43: return "Squash"
case 44: return "Stair Climbing"
case 45: return "Surfing Sports"
case 46: return "Swimming"
case 47: return "Table Tennis"
case 48: return "Tennis"
case 49: return "Track and Field"
case 50: return "Traditional Strength Training"
case 51: return "Volleyball"
case 52: return "Walking"
case 53: return "Water Fitness"
case 54: return "Water Polo"
case 55: return "Water Sports"
case 56: return "Wrestling"
case 57: return "Yoga"
case 58: return "Barre"
case 59: return "Core Training"
case 60: return "Cross Country Skiing"
case 61: return "Downhill Skiing"
case 62: return "Flexibility"
case 63: return "High Intensity Interval Training"
case 64: return "Jump Rope"
case 65: return "Kickboxing"
case 66: return "Pilates"
case 67: return "Snowboarding"
case 68: return "Stairs"
case 69: return "Step Training"
case 70: return "Wheelchair Walk Pace"
case 71: return "Wheelchair Run Pace"
case 72: return "Tai Chi"
case 73: return "Mixed Cardio"
case 74: return "Hand Cycling"
case 75: return "Disc Sports"
case 76: return "Fitness Gaming"
case 77: return "Cardio Dance"
case 78: return "Social Dance"
case 79: return "Pickleball"
case 80: return "Cooldown"
case 82: return "Swim Bike Run"
case 83: return "Transition"
case 84: return "Underwater Diving"
case 3000: return "Other"
default: return "UNKNOWN"
}
}
}
Upvotes: 0
Reputation: 51
extension HKWorkoutActivityType {
var name: String {
switch self {
case .americanFootball: return "American Football"
case .archery: return "Archery"
case .australianFootball: return "Australian Football"
case .badminton: return "Badminton"
case .baseball: return "Baseball"
case .basketball: return "Basketball"
case .bowling: return "Bowling"
case .boxing: return "Boxing"
case .climbing: return "Climbing"
case .cricket: return "Cricket"
case .crossTraining: return "Cross Training"
case .curling: return "Curling"
case .cycling: return "Cycling"
case .dance: return "Dance"
case .danceInspiredTraining: return "Dance Inspired Training"
case .elliptical: return "Elliptical"
case .equestrianSports: return "Equestrian Sports"
case .fencing: return "Fencing"
case .fishing: return "Fishing"
case .functionalStrengthTraining: return "Functional Strength Training"
case .golf: return "Golf"
case .gymnastics: return "Gymnastics"
case .handball: return "Handball"
case .hiking: return "Hiking"
case .hockey: return "Hockey"
case .hunting: return "Hunting"
case .lacrosse: return "Lacrosse"
case .martialArts: return "Martial Arts"
case .mindAndBody: return "Mind and Body"
case .mixedMetabolicCardioTraining: return "Mixed Metabolic Cardio Training"
case .paddleSports: return "Paddle Sports"
case .play: return "Play"
case .preparationAndRecovery: return "Preparation and Recovery"
case .racquetball: return "Racquetball"
case .rowing: return "Rowing"
case .rugby: return "Rugby"
case .running: return "Running"
case .sailing: return "Sailing"
case .skatingSports: return "Skating Sports"
case .snowSports: return "Snow Sports"
case .soccer: return "Soccer"
case .softball: return "Softball"
case .squash: return "Squash"
case .stairClimbing: return "Stair Climbing"
case .surfingSports: return "Surfing Sports"
case .swimming: return "Swimming"
case .tableTennis: return "Table Tennis"
case .tennis: return "Tennis"
case .trackAndField: return "Track and Field"
case .traditionalStrengthTraining: return "Traditional Strength Training"
case .volleyball: return "Volleyball"
case .walking: return "Walking"
case .waterFitness: return "Water Fitness"
case .waterPolo: return "Water Polo"
case .waterSports: return "Water Sports"
case .wrestling: return "Wrestling"
case .yoga: return "Yoga"
// - iOS 10
case .barre: return "Barre"
case .coreTraining: return "Core Training"
case .crossCountrySkiing: return "Cross Country Skiing"
case .downhillSkiing: return "Downhill Skiing"
case .flexibility: return "Flexibility"
case .highIntensityIntervalTraining: return "High Intensity Interval Training"
case .jumpRope: return "Jump Rope"
case .kickboxing: return "Kickboxing"
case .pilates: return "Pilates"
case .snowboarding: return "Snowboarding"
case .stairs: return "Stairs"
case .stepTraining: return "Step Training"
case .wheelchairWalkPace: return "Wheelchair Walk Pace"
case .wheelchairRunPace: return "Wheelchair Run Pace"
// - iOS 11
case .taiChi: return "Tai Chi"
case .mixedCardio: return "Mixed Cardio"
case .handCycling: return "Hand Cycling"
// - iOS 13
case .discSports: return "Disc Sports"
case .fitnessGaming: return "Fitness Gaming"
// - iOS 14
case .cardioDance: return "Cardio Dance"
case .socialDance: return "Social Dance"
case .pickleball: return "Pickleball"
case .cooldown: return "Cooldown"
// - Other
case .other: return "Other"
@unknown default: return "Other"
}
}
}
The actual version of George Green's answer for iOS 14 as well
Upvotes: 5
Reputation: 4905
For anyone still coming across this now, I have put together a simple HKWorkoutActivityType
extension in swift to allow mapping to these types to user friendly strings, and also to appropriate emoji representations where available: https://github.com/georgegreenoflondon/HKWorkoutActivityType-Descriptions/.
The relevant snippet for this question being:
extension HKWorkoutActivityType {
/*
Simple mapping of available workout types to a human readable name.
*/
var name: String {
switch self {
case .americanFootball: return "American Football"
case .archery: return "Archery"
case .australianFootball: return "Australian Football"
case .badminton: return "Badminton"
case .baseball: return "Baseball"
case .basketball: return "Basketball"
case .bowling: return "Bowling"
case .boxing: return "Boxing"
case .climbing: return "Climbing"
case .crossTraining: return "Cross Training"
case .curling: return "Curling"
case .cycling: return "Cycling"
case .dance: return "Dance"
case .danceInspiredTraining: return "Dance Inspired Training"
case .elliptical: return "Elliptical"
case .equestrianSports: return "Equestrian Sports"
case .fencing: return "Fencing"
case .fishing: return "Fishing"
case .functionalStrengthTraining: return "Functional Strength Training"
case .golf: return "Golf"
case .gymnastics: return "Gymnastics"
case .handball: return "Handball"
case .hiking: return "Hiking"
case .hockey: return "Hockey"
case .hunting: return "Hunting"
case .lacrosse: return "Lacrosse"
case .martialArts: return "Martial Arts"
case .mindAndBody: return "Mind and Body"
case .mixedMetabolicCardioTraining: return "Mixed Metabolic Cardio Training"
case .paddleSports: return "Paddle Sports"
case .play: return "Play"
case .preparationAndRecovery: return "Preparation and Recovery"
case .racquetball: return "Racquetball"
case .rowing: return "Rowing"
case .rugby: return "Rugby"
case .running: return "Running"
case .sailing: return "Sailing"
case .skatingSports: return "Skating Sports"
case .snowSports: return "Snow Sports"
case .soccer: return "Soccer"
case .softball: return "Softball"
case .squash: return "Squash"
case .stairClimbing: return "Stair Climbing"
case .surfingSports: return "Surfing Sports"
case .swimming: return "Swimming"
case .tableTennis: return "Table Tennis"
case .tennis: return "Tennis"
case .trackAndField: return "Track and Field"
case .traditionalStrengthTraining: return "Traditional Strength Training"
case .volleyball: return "Volleyball"
case .walking: return "Walking"
case .waterFitness: return "Water Fitness"
case .waterPolo: return "Water Polo"
case .waterSports: return "Water Sports"
case .wrestling: return "Wrestling"
case .yoga: return "Yoga"
// iOS 10
case .barre: return "Barre"
case .coreTraining: return "Core Training"
case .crossCountrySkiing: return "Cross Country Skiing"
case .downhillSkiing: return "Downhill Skiing"
case .flexibility: return "Flexibility"
case .highIntensityIntervalTraining: return "High Intensity Interval Training"
case .jumpRope: return "Jump Rope"
case .kickboxing: return "Kickboxing"
case .pilates: return "Pilates"
case .snowboarding: return "Snowboarding"
case .stairs: return "Stairs"
case .stepTraining: return "Step Training"
case .wheelchairWalkPace: return "Wheelchair Walk Pace"
case .wheelchairRunPace: return "Wheelchair Run Pace"
// iOS 11
case .taiChi: return "Tai Chi"
case .mixedCardio: return "Mixed Cardio"
case .handCycling: return "Hand Cycling"
// iOS 13
case .discSports: return "Disc Sports"
case .fitnessGaming: return "Fitness Gaming"
// Catch-all
default: return "Other"
}
}
}
Upvotes: 26
Reputation: 48125
You can just write your own parser, take string
from this page https://developer.apple.com/documentation/healthkit/hkworkoutactivitytype
We should write generated code, and run that in iOS app. This way the rawValue
of HKWorkoutActivityType
is always correct and up to date
private static func makeCode() {
let pattern = "case \\w*"
let range = NSMakeRange(0, string.count-1)
let regex = try! NSRegularExpression(pattern: pattern, options: [])
regex
.matches(in: string, options: [], range: range)
.forEach({ result in
let start = string.index(string.startIndex, offsetBy: result.range.lowerBound)
let end = string.index(string.startIndex, offsetBy: result.range.upperBound)
let substring = String(string[start...end])
let name = substring
.replacingOccurrences(of: "case", with: "")
.replacingOccurrences(of: " ", with: "")
.replacingOccurrences(of: "\n", with: "")
print("dictionary[HKWorkoutActivityType.\(name).rawValue] = \"\(name)\"")
})
}
Run the generated code in iOS app, we can get
(lldb) po dictionary
▿ 75 elements
▿ 0 : 2 elements
- key : 36
- value : "rugby"
▿ 1 : 2 elements
- key : 71
- value : "wheelchairRunPace"
▿ 2 : 2 elements
- key : 46
- value : "swimming"
▿ 3 : 2 elements
- key : 20
- value : "functionalStrengthTraining"
▿ 4 : 2 elements
- key : 55
- value : "waterSports"
▿ 5 : 2 elements
- key : 40
- value : "snowSports"
▿ 6 : 2 elements
- key : 63
- value : "highIntensityIntervalTraining"
▿ 7 : 2 elements
- key : 69
- value : "stepTraining"
▿ 8 : 2 elements
- key : 13
- value : "cycling"
▿ 9 : 2 elements
- key : 22
- value : "gymnastics"
▿ 10 : 2 elements
- key : 43
- value : "squash"
▿ 11 : 2 elements
- key : 62
- value : "flexibility"
▿ 12 : 2 elements
- key : 35
- value : "rowing"
▿ 13 : 2 elements
- key : 65
- value : "kickboxing"
▿ 14 : 2 elements
- key : 16
- value : "elliptical"
▿ 15 : 2 elements
- key : 34
- value : "racquetball"
▿ 16 : 2 elements
- key : 32
- value : "play"
▿ 17 : 2 elements
- key : 3000
- value : "other"
▿ 18 : 2 elements
- key : 18
- value : "fencing"
▿ 19 : 2 elements
- key : 60
- value : "crossCountrySkiing"
▿ 20 : 2 elements
- key : 52
- value : "walking"
▿ 21 : 2 elements
- key : 11
- value : "crossTraining"
▿ 22 : 2 elements
- key : 66
- value : "pilates"
▿ 23 : 2 elements
- key : 58
- value : "barre"
▿ 24 : 2 elements
- key : 45
- value : "surfingSports"
▿ 25 : 2 elements
- key : 50
- value : "traditionalStrengthTraining"
▿ 26 : 2 elements
- key : 23
- value : "handball"
▿ 27 : 2 elements
- key : 3
- value : "australianFootball"
▿ 28 : 2 elements
- key : 49
- value : "trackAndField"
▿ 29 : 2 elements
- key : 30
- value : "mixedMetabolicCardioTraining"
▿ 30 : 2 elements
- key : 27
- value : "lacrosse"
▿ 31 : 2 elements
- key : 28
- value : "martialArts"
▿ 32 : 2 elements
- key : 29
- value : "mindAndBody"
▿ 33 : 2 elements
- key : 15
- value : "danceInspiredTraining"
▿ 34 : 2 elements
- key : 4
- value : "badminton"
▿ 35 : 2 elements
- key : 54
- value : "waterPolo"
▿ 36 : 2 elements
- key : 25
- value : "hockey"
▿ 37 : 2 elements
- key : 7
- value : "bowling"
▿ 38 : 2 elements
- key : 68
- value : "stairs"
▿ 39 : 2 elements
- key : 70
- value : "wheelchairWalkPace"
▿ 40 : 2 elements
- key : 59
- value : "coreTraining"
▿ 41 : 2 elements
- key : 33
- value : "preparationAndRecovery"
▿ 42 : 2 elements
- key : 21
- value : "golf"
▿ 43 : 2 elements
- key : 51
- value : "volleyball"
▿ 44 : 2 elements
- key : 38
- value : "sailing"
▿ 45 : 2 elements
- key : 24
- value : "hiking"
▿ 46 : 2 elements
- key : 1
- value : "americanFootball"
▿ 47 : 2 elements
- key : 44
- value : "stairClimbing"
▿ 48 : 2 elements
- key : 41
- value : "soccer"
▿ 49 : 2 elements
- key : 37
- value : "running"
▿ 50 : 2 elements
- key : 17
- value : "equestrianSports"
▿ 51 : 2 elements
- key : 42
- value : "softball"
▿ 52 : 2 elements
- key : 47
- value : "tableTennis"
▿ 53 : 2 elements
- key : 2
- value : "archery"
▿ 54 : 2 elements
- key : 6
- value : "basketball"
▿ 55 : 2 elements
- key : 57
- value : "yoga"
▿ 56 : 2 elements
- key : 72
- value : "taiChi"
▿ 57 : 2 elements
- key : 73
- value : "mixedCardio"
▿ 58 : 2 elements
- key : 67
- value : "snowboarding"
▿ 59 : 2 elements
- key : 56
- value : "wrestling"
▿ 60 : 2 elements
- key : 53
- value : "waterFitness"
▿ 61 : 2 elements
- key : 5
- value : "baseball"
▿ 62 : 2 elements
- key : 8
- value : "boxing"
▿ 63 : 2 elements
- key : 9
- value : "climbing"
▿ 64 : 2 elements
- key : 39
- value : "skatingSports"
▿ 65 : 2 elements
- key : 26
- value : "hunting"
▿ 66 : 2 elements
- key : 19
- value : "fishing"
▿ 67 : 2 elements
- key : 48
- value : "tennis"
▿ 68 : 2 elements
- key : 61
- value : "downhillSkiing"
▿ 69 : 2 elements
- key : 64
- value : "jumpRope"
▿ 70 : 2 elements
- key : 14
- value : "dance"
▿ 71 : 2 elements
- key : 74
- value : "handCycling"
▿ 72 : 2 elements
- key : 12
- value : "curling"
▿ 73 : 2 elements
- key : 10
- value : "cricket"
▿ 74 : 2 elements
- key : 31
- value : "paddleSports"
Upvotes: 0
Reputation: 5536
Unfortunately, apple only provides an enum and you would have to implement your own names. A good idea is to lazily instantiate a dictionary including keys of NSNumber
(Object wrapped NSInteger
) and values of strings. Should be quite easy to make and unfortunately the only solution.
Upvotes: 5