如何在healthkit中阅读HKWorkoutActivityType.Running?

时间:2015-11-08 02:22:22

标签: iphone swift2 ios9 health-kit

我尝试使用以下代码从healthapp读取数据,但是我得到的结果为nil值,并且错误无效HKObjectType HKQuantityTypeIdentifierDistanceWalkingRunning for keyPath workoutType。

mac版:10.10.5 xcode版本:7.1

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)

1 个答案:

答案 0 :(得分:1)

使用它:

let query = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: workoutPredicate,
        limit: 0, sortDescriptors: [startDateSort]) {
    // ...
}

使用sampleType,您说要选择锻炼。 谓词确定用于选择的锻炼属性。

您告诉healthSore选择带有锻炼属性的运行样本。这不合适。