Last active
May 2, 2019 03:59
-
-
Save hungtruong/a45a91a9d598a448db7ce3947d70304d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let calories = message["calories"] as? Int, | |
let distance = message["distance"] as? Int, | |
let start = message["start"] as? Date, | |
let end = message["end"] as? Date | |
{ | |
let calorieQuantityType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)! | |
let calorieQuantity = HKQuantity(unit: .kilocalorie(), doubleValue: Double(calories)) | |
let caloriesBurned = HKQuantitySample(type: calorieQuantityType, quantity: calorieQuantity, start: start, end: end) | |
let distanceQuantityType = HKQuantityType.quantityType(forIdentifier: .distanceCycling)! | |
let distanceQuantity = HKQuantity(unit: .meter(), doubleValue: Double(distance)) | |
let distanceCycled = HKQuantitySample(type: distanceQuantityType, quantity: distanceQuantity, start: start, end: end) | |
self.workoutBuilder.add([caloriesBurned, distanceCycled]) { (_, _) in | |
self.endWorkout(date: end) | |
} | |
} | |
if let segment = message["segmentName"] as? String, | |
let dateInterval = message["dateInterval"] as? DateInterval { | |
let workoutEvent = HKWorkoutEvent(type: .segment, dateInterval: dateInterval, | |
metadata: ["Segment Name" : segment]) | |
self.workoutBuilder.addWorkoutEvents([workoutEvent]) { (success, error) in | |
print(success ? "Success saving segment" : error as Any) | |
} | |
} | |
if let workoutName = message["workoutName"] as? String { | |
let metadata = [HKMetadataKeyWorkoutBrandName: workoutName] | |
self.workoutBuilder.addMetadata(metadata) { (success, error) in | |
print(success ? "Success saving metadata" : error as Any) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment