2

I am able to get basic data of activities like duration, totalEnergyBurned etc. But how can I access meta data like HKWasUserEntered startDate ?

Here is my code so far

var predicate = HKQuery.predicateForWorkoutsWithWorkoutActivityType(HKWorkoutActivityType.Cycling)
 let sampleQuery = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor])
    { (sampleQuery, results, error ) -> Void in

        if let queryError = error {
            print( "There was an error while reading the samples: \(queryError.localizedDescription)")
        }
        else
        {
            print(results!)
          for samples: HKSample in results! {
                var workout: HKWorkout = (samples as! HKWorkout)
                print("workout Distance is \(workout.totalDistance)")
                print("workout Energy Burned is \(workout.totalEnergyBurned)")
                print("workout Duration in seconds is \(workout.duration)")

            }
        }
        completion(results,error)
    }

    executeQuery(sampleQuery)

When I print results I am getting this in return

[ (46) "Health" (9.3.2) metadata: { HKWasUserEntered = 1; } 2016-07-18 10:58:00 +0500 2016-07-18 11:58:00 +0500]

so basically how can I fetch HKWasUserEntered and dates from this ?

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
Byte
  • 629
  • 2
  • 11
  • 29

3 Answers3

0

There is a metadata dictionary property on HKSample. You could print the value like this, for example:

print("workout was user entered \(workout.metadata[HKWasUserEntered])")

Allan
  • 7,039
  • 1
  • 16
  • 26
0

Try the following:

print("was user entered --> \(workout.metadata?[HKMetadataKeyWasUserEntered])")
Blue
  • 22,608
  • 7
  • 62
  • 92
0

I understood how work with metadata.

            if let metadata = workout.metadata {
                if let  mataTemperature = metadata[HKMetadataKeyWeatherTemperature] {
                    if let quantityTemperature = mataTemperature as? HKQuantity {
                        let celsius = quantityTemperature.doubleValue(for: HKUnit.degreeCelsius())
                        print(celsius)
                    }
                }