I am invoking Healthkit through React Native's NativeModules.
I can get the Authorisation part to work.
healthkit.m:
RCT_EXTERN_METHOD(requestAuthorization)
healthkit.swift:
@objc
func requestAuthorization() {
let writeDataTypes : Set<HKSampleType> = [ HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!,
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic)!,
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic)!,
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMassIndex)!,
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose)!,
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyTemperature)!
]
let readDataTypes : Set = [HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.biologicalSex)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMassIndex)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyTemperature)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.bloodType)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth)!]
healthStore.requestAuthorization(toShare: writeDataTypes, read: readDataTypes) { (success, error) in
if (self.healthStore.authorizationStatus(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose)!) == .sharingAuthorized) {
print("Permission Granted to Access bloodGlucose")
} else {
print("Permission Denied to Access bloodGlucose")
}
if !success {
// Handle the error here.
}
}
}
But the Query part doesn't work and it throws the below error.
healthkit.m
RCT_EXTERN_METHOD(getHeartRate: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject)
healthkit.swift:
@objc
func getHeartRate(_ resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock) {
let heartRate = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)
let startOfDate = Calendar.current.startOfDay(for: startDate)
let endOfDate = Calendar.current.endOfDay(for: endDate)
print("dat:",startOfDate, endOfDate)
let predicate = HKQuery.predicateForSamples(withStart: startOfDate, end: endOfDate, options: .strictStartDate)
let query = HKSampleQuery(sampleType: heartRate!, predicate: predicate, limit: 20, sortDescriptors: nil) { (query, results, error) in
for result in results as! [HKQuantitySample] {
..................
}
What is wrong with the above code???
Could be it gets confused by the "get" prefix in the method?
@objc(getHeartRate:rejecter:)
func getHeartRate(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock)