I am working on my first ResearchKit project. I am trying to get heart rate data through apple's HealthKit. I am testing the program on my phone, and I have the apple watch with Health data, it should be available. The walking task starts and finishes successfully, and I am able to parse through the result files. But I am finding that the result files only contain physical sensor data (accelerometer and gyro) and NOT any health data.
What concerns me a little is that I see these 2 warning on the console output when the walking task starts:
ORKSample[511:80256] [ResearchKit][Warning] __82-[ORKTaskViewController requestHealthStoreAccessWithReadTypes:writeTypes:handler:]_block_invoke Health access: error=(null)
2016-04-07 16:31:28.097 ORKSample[511:80630] [ResearchKit][Warning] __59-[ORKTaskViewController requestPedometerAccessWithHandler:]_block_invoke Pedometer access: error=(null)
It seems that _block_invole Health access
and _block_invoke Pedometer access
can't be good.
Here is code that I use to authorize the health data:
import ResearchKit
import HealthKit
class HealthDataStep: ORKInstructionStep {
// MARK: Properties
let healthDataItemsToRead: Set<HKObjectType> = [
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!
]
let healthDataItemsToWrite: Set<HKSampleType> = [
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
HKObjectType.workoutType(),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!
]
// MARK: Initialization
override init(identifier: String) {
super.init(identifier: identifier)
title = NSLocalizedString("Health Data", comment: "")
text = NSLocalizedString("On the next screen, you will be prompted to grant access to read and write some of your general and health information, such as height, weight, and steps taken so you don't have to enter it again.", comment: "")
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: Convenience
func getHealthAuthorization(completion: (success: Bool, error: NSError?) -> Void) {
guard HKHealthStore.isHealthDataAvailable() else {
let error = NSError(domain: "com.example.apple-samplecode.ORKSample", code: 2, userInfo: [NSLocalizedDescriptionKey: "Health data is not available on this device."])
completion(success: false, error:error)
return
}
// Get authorization to access the data
HKHealthStore().requestAuthorizationToShareTypes(healthDataItemsToWrite, readTypes: healthDataItemsToRead) { (success, error) -> Void in
completion(success:success, error:error)
}
}
}
To implement the walking task I use this code, very simple:
public var TimedWalkTask: ORKOrderedTask {
return ORKOrderedTask.fitnessCheckTaskWithIdentifier("WalkTask",intendedUseDescription: nil,walkDuration: 15 as NSTimeInterval, restDuration: 15 as NSTimeInterval,options: .None)
}
Just wondering if anyone knows if I am missing something. The program seems to run correctly and return results, but the results don't contain that health data that I'm looking for.
Just for your information, I have attached a screen shot of the health permissions for my app in the iphone settings: