3

I am creating an Fitness health app in which, I have to show user real time calories and their heart rate from Health kit. I get the permissions. But I don't know how to fetch the calories and heart rate.

Here is the permission of code.

static let healthKitStore = HKHealthStore()

// MARK:- Health Kit Permissions :-
static func authorizeHealthKit() {

    // HKQuantityTypeIdentifierBodyMass - For Weight

    var shareTypes = Set<HKSampleType>()
    shareTypes.insert(HKSampleType.workoutType())
    shareTypes.insert(HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!)
    shareTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!)
    shareTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!)


    var readTypes = Set<HKObjectType>()
    readTypes.insert(HKObjectType.workoutType())
    readTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!)
    readTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!)
    readTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!)

    healthKitStore.requestAuthorizationToShareTypes(shareTypes, readTypes: readTypes) { (success, error) -> Void in
        if success {
            print("success")

        } else {
            print("failure")

        }

        if let error = error {

            print(error)
        }
    }

}

After getting success I need the calories and heart rate which function or which code I have to use. Like Fitbit Apis return the data or there is Apple Api's which will return the data?

halfer
  • 18,701
  • 13
  • 79
  • 158
Mandeep Singh
  • 2,600
  • 1
  • 16
  • 27

1 Answers1

1

Check this code:

let tHeartRate = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)
            let tHeartRateQuery = HKSampleQuery(sampleType: tHeartRate!, predicate:.None, limit: 0, sortDescriptors: nil) { query, results, error in

                if results?.count > 0
                {
                    var string:String = ""
                    for result in results as! [HKQuantitySample]
                    {
                        let HeartRate = result.quantity
                        string = "\(HeartRate)"
                        print(string)
                    }
                }
}
self.healthKitStore.executeQuery(tHeartRateQuery)
VJVJ
  • 415
  • 2
  • 17
  • i have to call this method after i get the permissions am i right – Mandeep Singh Nov 22 '16 at 11:00
  • it will return the data like this 1.73333 count/s, 1.65 count/s, 2.48333 count/s. But heart rate should be like 98, 81, 103 in this format. – Mandeep Singh Nov 22 '16 at 11:12
  • @MandeepSingh get the numerical component from the string and multiply with 60 to get the result in the format (98,103,98,etc) refer to this link to get the numerical part http://stackoverflow.com/questions/30342744/swift-how-to-get-integer-from-string-and-convert-it-into-integer – Md. Ibrahim Hassan Nov 22 '16 at 11:45