我正在做我的第一个 ResearchKit 项目。我正在尝试通过苹果的 HealthKit 获取心率数据。我正在手机上测试该程序,并且我有带有健康数据的 Apple Watch,它应该可用。步行任务成功启动和完成,我能够解析结果文件。但我发现结果文件仅包含物理传感器数据(加速度计和陀螺仪),而不包含任何健康数据。
让我有点担心的是,当步行任务开始时,我在控制台输出上看到了这两个警告:
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)
好像_block_invole Health access
也不_block_invoke Pedometer access
是很好。
这是我用来授权健康数据的代码:
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)
}
}
}
为了实现步行任务,我使用了这段代码,非常简单:
public var TimedWalkTask: ORKOrderedTask {
return ORKOrderedTask.fitnessCheckTaskWithIdentifier("WalkTask",intendedUseDescription: nil,walkDuration: 15 as NSTimeInterval, restDuration: 15 as NSTimeInterval,options: .None)
}
只是想知道是否有人知道我是否遗漏了什么。该程序似乎运行正确并返回结果,但结果不包含我正在寻找的健康数据。