How to read heart rate from iOS HealthKit app using Swift?
Asked Answered
S

5

11

I'm using the following Swift code.

let sampleType : HKSampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)
let nowDate: NSDate = NSDate()
var calendar: NSCalendar = NSCalendar.autoupdatingCurrentCalendar()

let yearMonthDay: NSCalendarUnit = NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit

var components: NSDateComponents = calendar.components(yearMonthDay , fromDate: nowDate)
var beginOfDay : NSDate = calendar.dateFromComponents(components)!
var predicate : NSPredicate = HKQuery.predicateForSamplesWithStartDate(beginOfDay, endDate: nowDate, options: HKQueryOptions.StrictStartDate)

let squery: HKStatisticsQuery = HKStatisticsQuery(quantityType: sampleType, quantitySamplePredicate: predicate, options: HKStatisticsOptions.None) { (qurt, resul, errval) -> Void in

    dispatch_async( dispatch_get_main_queue(), { () -> Void in
        var quantity : HKQuantity = result.averageQuantity;
        var beats : double = quantity.doubleValueForUnit(HKUnit.heartBeatsPerMinuteUnit())
        // [quantity doubleValueForUnit:[HKUnit heartBeatsPerMinuteUnit]];
         self.txtfldHeartRate.text = "\(beats)"
    })

}

healthManager.healthKitStore.executeQuery(squery)

I get the following error message:

Cannot find an initializer for type 'HKStatisticsQuery' that accepts an argument list of type '(quantityType: HKSampleType, quantitySamplePredicate: NSPredicate, options: HKStatisticsOptions, (_, _, _) -> Void)'

Please advise me how to resolve this issue.

Selfoperating answered 10/9, 2015 at 4:50 Comment(0)
M
31

Reading data from ViewController (NOT apple watch extension)

Swift 5

let health: HKHealthStore = HKHealthStore()
let heartRateUnit:HKUnit = HKUnit(from: "count/min")
let heartRateType:HKQuantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!
    var heartRateQuery:HKSampleQuery?

/*Method to get todays heart rate - this only reads data from health kit. */
 func getTodaysHeartRates() {
    //predicate
    let calendar = NSCalendar.current
    let now = NSDate()
    let components = calendar.dateComponents([.year, .month, .day], from: now as Date)
    
    guard let startDate:NSDate = calendar.date(from: components) as NSDate? else { return }
    var dayComponent    = DateComponents()
    dayComponent.day    = 1
    let endDate:NSDate? = calendar.date(byAdding: dayComponent, to: startDate as Date) as NSDate?
    let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: endDate as Date?, options: [])

    //descriptor
    let sortDescriptors = [
                            NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
                          ]
    
    heartRateQuery = HKSampleQuery(sampleType: heartRateType, predicate: predicate, limit: 25, sortDescriptors: sortDescriptors, resultsHandler: { (query, results, error) in
        guard error == nil else { print("error"); return }

        self.printHeartRateInfo(results: results)
    }) //eo-query
    
    health.execute(heartRateQuery!)
 }//eom

/*used only for testing, prints heart rate info */
private func printHeartRateInfo(results:[HKSample]?)
{
    for (_, sample) in results!.enumerated() {
        guard let currData:HKQuantitySample = sample as? HKQuantitySample else { return }

        print("[\(sample)]")
        print("Heart Rate: \(currData.quantity.doubleValue(for: heartRateUnit))")
        print("quantityType: \(currData.quantityType)")
        print("Start Date: \(currData.startDate)")
        print("End Date: \(currData.endDate)")
        print("Metadata: \(currData.metadata)")
        print("UUID: \(currData.uuid)")
        print("Source: \(currData.sourceRevision)")
        print("Device: \(currData.device)")
        print("---------------------------------\n")
    }//eofl
}//eom

Swift 3

let health: HKHealthStore = HKHealthStore()
let heartRateUnit:HKUnit = HKUnit(fromString: "count/min")
let heartRateType:HKQuantityType   = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
var heartRateQuery:HKSampleQuery?


/*Method to get todays heart rate - this only reads data from health kit. */
 func getTodaysHeartRates()
    {
        //predicate
        let calendar = NSCalendar.currentCalendar()
        let now = NSDate()
        let components = calendar.components([.Year,.Month,.Day], fromDate: now)
        guard let startDate:NSDate = calendar.dateFromComponents(components) else { return }
        let endDate:NSDate? = calendar.dateByAddingUnit(.Day, value: 1, toDate: startDate, options: [])
        let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
        
        //descriptor
        let sortDescriptors = [
                                NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
                              ]
        
        heartRateQuery = HKSampleQuery(sampleType: heartRateType,
                                        predicate: predicate,
                                        limit: 25,
                                        sortDescriptors: sortDescriptors)
            { (query:HKSampleQuery, results:[HKSample]?, error:NSError?) -> Void in
                
                guard error == nil else { print("error"); return }

                //self.printHeartRateInfo(results)
                
                self.updateHistoryTableViewContent(results)

        }//eo-query
        health.executeQuery(heartRateQuery!)
        
   }//eom

/*used only for testing, prints heart rate info */
private func printHeartRateInfo(results:[HKSample]?)
    {
        for(var iter = 0 ; iter < results!.count; iter++)
        {
            guard let currData:HKQuantitySample = results![iter] as? HKQuantitySample else { return }
            
            print("[\(iter)]")
            print("Heart Rate: \(currData.quantity.doubleValueForUnit(heartRateUnit))")
            print("quantityType: \(currData.quantityType)")
            print("Start Date: \(currData.startDate)")
            print("End Date: \(currData.endDate)")
            print("Metadata: \(currData.metadata)")
            print("UUID: \(currData.UUID)")
            print("Source: \(currData.sourceRevision)")
            print("Device: \(currData.device)")
            print("---------------------------------\n")
        }//eofl
    }//eom

Reading data with apple watch extension:

to execute query in apple watch, do the following:

        heartRateQuery = self.createStreamingQuery()
        health.executeQuery(heartRateQuery!)

don't forget the properties:

let health: HKHealthStore = HKHealthStore()
let heartRateUnit:HKUnit = HKUnit(fromString: "count/min")
let heartRateType:HKQuantityType   = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
var heartRateQuery:HKQuery?

/*The below methods has no Limit, query for heart infinitely once its the query its executed */

private func createStreamingQuery() -> HKQuery
    {
        let queryPredicate  = HKQuery.predicateForSamplesWithStartDate(NSDate(), endDate: nil, options: .None)
        
    let query:HKAnchoredObjectQuery = HKAnchoredObjectQuery(type: self.heartRateType, predicate: queryPredicate, anchor: nil, limit: Int(HKObjectQueryNoLimit))
    { (query:HKAnchoredObjectQuery, samples:[HKSample]?, deletedObjects:[HKDeletedObject]?, anchor:HKQueryAnchor?, error:NSError?) -> Void in
    
        if let errorFound:NSError = error
        {
            print("query error: \(errorFound.localizedDescription)")
        }
        else
        {
            //printing heart rate
             if let samples = samples as? [HKQuantitySample]
              {
                 if let quantity = samples.last?.quantity
                 {
                     print("\(quantity.doubleValueForUnit(heartRateUnit))")
                 }
               }
        }
    }//eo-query
    
    query.updateHandler =
        { (query:HKAnchoredObjectQuery, samples:[HKSample]?, deletedObjects:[HKDeletedObject]?, anchor:HKQueryAnchor?, error:NSError?) -> Void in
            
            if let errorFound:NSError = error
            {
                print("query-handler error : \(errorFound.localizedDescription)")
            }
            else
            {
                  //printing heart rate
                  if let samples = samples as? [HKQuantitySample]
                  {
                       if let quantity = samples.last?.quantity
                       {
                          print("\(quantity.doubleValueForUnit(heartRateUnit))")
                       }
                  }
            }//eo-non_error
    }//eo-query-handler
    
    return query
}//eom

How to request authorization?

func requestAuthorization()
    {
    //reading
    let readingTypes:Set = Set( [heartRateType] )
    
    //writing
    let writingTypes:Set = Set( [heartRateType] )
    
    //auth request
    health.requestAuthorizationToShareTypes(writingTypes, readTypes: readingTypes) { (success, error) -> Void in
        
        if error != nil
        {
            print("error \(error?.localizedDescription)")
        }
        else if success
        {
            
        }
    }//eo-request
}//eom
Merkley answered 16/2, 2016 at 19:29 Comment(0)
E
6

HKStatisticsQuery would not be my first choice for that. It is used for statistical calculations (i.e. minimum, maximum, average, sum).

You can use a simple HKQuery:

  public func fetchLatestHeartRateSample(
    completion: @escaping (_ samples: [HKQuantitySample]?) -> Void) {

    /// Create sample type for the heart rate
    guard let sampleType = HKObjectType
      .quantityType(forIdentifier: .heartRate) else {
        completion(nil)
      return
    }

    /// Predicate for specifiying start and end dates for the query
    let predicate = HKQuery
      .predicateForSamples(
        withStart: Date.distantPast,
        end: Date(),
        options: .strictEndDate)

    /// Set sorting by date.
    let sortDescriptor = NSSortDescriptor(
      key: HKSampleSortIdentifierStartDate,
      ascending: false)

    /// Create the query
    let query = HKSampleQuery(
      sampleType: sampleType,
      predicate: predicate,
      limit: Int(HKObjectQueryNoLimit),
      sortDescriptors: [sortDescriptor]) { (_, results, error) in

        guard error == nil else {
          print("Error: \(error!.localizedDescription)")
          return
        }


        completion(results as? [HKQuantitySample])
    }

    /// Execute the query in the health store
    let healthStore = HKHealthStore()
    healthStore.execute(query)
  }
Elrod answered 28/2, 2018 at 20:30 Comment(0)
A
0

Specifying types of constants and variables during an initialization is often redundant in Swift like in your case you has specified a parent HKSampleType type instead of its subclass HKQuantityType. So in your case just omit types declaration:

let sampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
let nowDate = NSDate()
var calendar = NSCalendar.autoupdatingCurrentCalendar()

If you use Swift 2.0 you should also utilize array like syntax in the next line:

let yearMonthDay: NSCalendarUnit = [NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day]
Ascription answered 10/9, 2015 at 6:52 Comment(0)
S
0

Try this way, moving in the completion handler seems to resolves this for me in Xcode 6.4-

let squery = HKStatisticsQuery(quantityType: sampleType, quantitySamplePredicate: predicate, options: HKStatisticsOptions.None, completionHandler: { (qurt, result, errval) -> Void in

  dispatch_async( dispatch_get_main_queue(), { () -> Void in

    var quantity : HKQuantity = result.averageQuantity();
    var beats : Double = quantity.doubleValueForUnit(HKUnit.atmosphereUnit())
    // [quantity doubleValueForUnit:[HKUnit heartBeatsPerMinuteUnit]];
  })
})

Note: I was seeing some compiler errors within closure, so have changed 2 lines to just ensure compilation-

var quantity : HKQuantity = result.averageQuantity();
var beats : Double = quantity.doubleValueForUnit(HKUnit.atmosphereUnit())
Sthilaire answered 10/9, 2015 at 8:36 Comment(0)
M
0

You should be using HKQuantityType instead of HKSampleType in your query:

let squery: HKStatisticsQuery = HKStatisticsQuery(quantityType: HKQuantityTypeIdentifierHeartRate, quantitySamplePredicate: predicate, options: HKStatisticsOptions.None) { (qurt, resul, errval) -> Void in
Meldon answered 18/8, 2016 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.