Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 可以从HealthKit中读取Apple Watch的目标(移动、跨步和站立)吗?_Ios_Apple Watch_Watchos - Fatal编程技术网

Ios 可以从HealthKit中读取Apple Watch的目标(移动、跨步和站立)吗?

Ios 可以从HealthKit中读取Apple Watch的目标(移动、跨步和站立)吗?,ios,apple-watch,watchos,Ios,Apple Watch,Watchos,可以从HealthKit中读取Apple Watch移动目标吗 我可以使用数量标识符HKQuantityTypeIdentifier.activeEnergyBurned检索移动值。我找不到移动目标的类似标识符。我得到了答案。可从HKActivitySummary访问移动目标 您应申请阅读HKActivitySummaryType的权限: let activitySummaryType = HKActivitySummaryType.activitySummaryType() let rea

可以从HealthKit中读取Apple Watch移动目标吗


我可以使用数量标识符HKQuantityTypeIdentifier.activeEnergyBurned检索移动值。我找不到移动目标的类似标识符。

我得到了答案。可从
HKActivitySummary
访问移动目标

您应申请阅读HKActivitySummaryType的权限:

 let activitySummaryType = HKActivitySummaryType.activitySummaryType()
 let readDataTypes: Set<HKObjectType> = [activitySummaryType]
 healthStore.requestAuthorization(toShare: nil, read: readDataTypes, completion: myCompletionHandler)
可从HKActivitySummary访问的其他活动摘要数据也可用


参考资料:


您的示例不完整。我将您的代码复制到一个新项目中,但无法使其正常工作。谢谢。使用以下方法启用健康工具包访问后,这对我来说非常有效:
let query = HKActivitySummaryQuery(predicate: myPredicate) { (query, summaries, error) -> Void in
    if error != nil {
        fatalError("*** Did not return a valid error object. ***")
    }

    if let activitySummaries = summaries {
        for summary in activitySummaries {
            print(summary.activeEnergyBurnedGoal)
            //do something with the summary here...
        }
    }
}
healthStore.execute(query)
//Declared globally
var healthStore: HKHealthStore?

func prepareHealthKit() {
    guard HKHealthStore.isHealthDataAvailable() else {
        return
    }

    var readTypes = Set<HKObjectType>()
    readTypes.insert(HKObjectType.activitySummaryType())

    healthStore = HKHealthStore()
    healthStore!.requestAuthorization(toShare: nil, read: readTypes) { (isSuccess, error) in
        /*
         Assuming you know the following steps:
         1. Start workout session: i.e. "HKWorkoutSession"
         2. Wait for delegate: i.e "workoutSession(_:didChangeTo:from:date:)"
         3. Start Query for Activity Summary in the delegate:
            i.e our "startQueryForActivitySummary()"
         */
    }
}
func startQueryForActivitySummary() {
    func createPredicate() -> NSPredicate? {
        let calendar = Calendar.autoupdatingCurrent

        var dateComponents = calendar.dateComponents([.year, .month, .day],
                                                     from: Date())
        dateComponents.calendar = calendar

        let predicate = HKQuery.predicateForActivitySummary(with: dateComponents)
        return predicate
    }

    let queryPredicate = createPredicate()
    let query = HKActivitySummaryQuery(predicate: queryPredicate) { (query, summaries, error) -> Void in
        if let summaries = summaries {
            for summary in summaries {
                let activeEnergyBurned = summary.activeEnergyBurned.doubleValue(for: HKUnit.kilocalorie())
                let activeEnergyBurnedGoal = summary.activeEnergyBurnedGoal.doubleValue(for: HKUnit.kilocalorie())
                let activeEnergyBurnGoalPercent = round(activeEnergyBurned/activeEnergyBurnedGoal)

                print(activeEnergyBurnGoalPercent)
            }
        }
    }

    healthStore?.execute(query)
}