How to get the name of HKWorkoutActivityType in HealthKit?
Asked Answered
P

4

14

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?

Pricking answered 11/5, 2015 at 18:42 Comment(0)
T
5

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.

Tannic answered 11/5, 2015 at 22:56 Comment(1)
FYI someone someone who made a constant wrapper here github.com/openmhealth/Granola/blob/master/Pod/Classes/…Wivern
C
23

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

}
Coniah answered 2/3, 2020 at 15:24 Comment(2)
Wow, you're a legend. Thanks for doing that! Saved me a lot of time :)Monumental
you're a legend indeedMattie
T
5

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.

Tannic answered 11/5, 2015 at 22:56 Comment(1)
FYI someone someone who made a constant wrapper here github.com/openmhealth/Granola/blob/master/Pod/Classes/…Wivern
S
3
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

Selfheal answered 13/3, 2022 at 7:51 Comment(3)
Did you just copy George Green's answer into your own answer? I'm confused as to how this is any different.Dekameter
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Flatfish
@Dekameter iOS 14 supported types were addedSelfheal
G
0

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"
Gull answered 16/10, 2018 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.