Iphone 如何阅读在healthkit中运行的HKWorkUtactivityType?
我尝试在下面的代码中从healthapp读取数据,但得到的结果为nil值,并且错误为无效的HKObjectType HKQuantityTypeIdentifierDistanceWalkingRunning for keyPath workoutType mac版本:10.10.5 xcode版本:7.1Iphone 如何阅读在healthkit中运行的HKWorkUtactivityType?,iphone,swift2,ios9,healthkit,Iphone,Swift2,Ios9,Healthkit,我尝试在下面的代码中从healthapp读取数据,但得到的结果为nil值,并且错误为无效的HKObjectType HKQuantityTypeIdentifierDistanceWalkingRunning for keyPath workoutType mac版本:10.10.5 xcode版本:7.1 let distanceType = HKObjectType.quantityTypeForIdentifier( HKQuantityTypeId
let distanceType =
HKObjectType.quantityTypeForIdentifier(
HKQuantityTypeIdentifierDistanceWalkingRunning)
let workoutPredicate = HKQuery.predicateForWorkoutsWithWorkoutActivityType(HKWorkoutActivityType.Running)
let startDateSort =
NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)
let query = HKSampleQuery(sampleType: distanceType!, predicate: workoutPredicate,
limit: 0, sortDescriptors: [startDateSort]) {
(sampleQuery, results, error) -> Void in
if let distanceSamples = results as? [HKQuantitySample] {
// process the detailed samples...
}
else {
// Perform proper error handling here...
print("*** An error occurred while adding a sample to " +
"the workout: \(error!.localizedDescription)")
abort()
}
}
// Execute the query
healthManager.healthKitStore.executeQuery(query)
下面是访问healthkit数据的代码
// 1. Set the types you want to read from HK Store
let typeOfRead = [HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierOxygenSaturation)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureDiastolic)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureSystolic)!]
let typeOfReads = NSSet(array: typeOfRead)
// 2. Set the types you want to write to HK Store
let typeOfWrite = [
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!
]
let typeOfWrites = NSSet(array: typeOfWrite)
使用以下命令:
let query = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: workoutPredicate,
limit: 0, sortDescriptors: [startDateSort]) {
// ...
}
在样本类型中,您表示希望选择训练。
谓词确定用于选择的训练属性
您告诉healthSore选择具有训练属性的跑步样本。那不合适