健康套件:如何设置“totalEnergyBurned”为了锻炼?

发布于 2025-01-15 06:36:21 字数 1172 浏览 1 评论 0原文

今天,我第一次使用 Apple Health Kit,并成功在 Health 中保存了一次锻炼,其中包含基本信息(活动类型、开始和结束)。

我的应用程序的基本功能是间隔计时器,您可以在其中创建自己的锻炼。现在,在下一步中,我要添加锻炼中燃烧的卡路里。该信息存储在属性“totalEnergyBurned”中。

我需要自己计算这个值还是如果用户戴着Apple Watch我可以直接查询这个值吗?或者如果有相应的记录,该值甚至会自动添加到锻炼中? (到目前为止我只在模拟器中测试了该应用程序,这就是为什么我无法回答这种可能性)。

我当前的代码:

func saveToHealthKit(entryID: String) {
    if HKHealthStore.isHealthDataAvailable() {
        let healthStore = HKHealthStore()

        if (healthStore.authorizationStatus(for: HKObjectType.workoutType()) == .sharingAuthorized && healthStore.authorizationStatus(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)!) == .sharingAuthorized) {
            let newWorkout = HKWorkout(activityType: HKWorkoutActivityType.highIntensityIntervalTraining, start: entry.date!, end: Date())

            healthStore.save(newWorkout) { success, error in
                guard success else {
                    // Perform proper error handling here.
                    return
                }

                // Add detail samples here.
            }
        }
    }
}

Today I worked for the first time with Apple Health Kit and successfully saved a workout in Health with the basic informations (activityType, start and end).

My app is in basic function an interval timer where you can create your own workout. Now in the next step I want to add the calories burned in the workout. This information is stored in the attribute 'totalEnergyBurned'.

Do I need to calculate this value myself or can I query this value directly if the user is wearing an Apple Watch? Or maybe the value is even automatically added to the workout if there is the corresponding record? (So far I have only tested the app in the simulator, which is why I can't answer this possibility).

My current code:

func saveToHealthKit(entryID: String) {
    if HKHealthStore.isHealthDataAvailable() {
        let healthStore = HKHealthStore()

        if (healthStore.authorizationStatus(for: HKObjectType.workoutType()) == .sharingAuthorized && healthStore.authorizationStatus(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)!) == .sharingAuthorized) {
            let newWorkout = HKWorkout(activityType: HKWorkoutActivityType.highIntensityIntervalTraining, start: entry.date!, end: Date())

            healthStore.save(newWorkout) { success, error in
                guard success else {
                    // Perform proper error handling here.
                    return
                }

                // Add detail samples here.
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网名女生简单气质 2025-01-22 06:36:21

我不确定我是否正确理解了您的情况,但如果用户使用 Apple Watch 并进行锻炼,您可以查询 HealthKit 中的 HKWorkout 对象,该对象包含 totalEnergyBurned 属性。

如果您没有查询此 HKWorkout 对象,那么我相信您可以使用谓词中用户锻炼的开始和结束日期时间来查询 activeEnergyBurned 数量类型。

I am not sure I understood your case correctly, but if the user is using an Apple Watch and does a workout you can query HealthKit for an HKWorkout object, which holds a totalEnergyBurned attribute.

If you are not querying this HKWorkout object, then I believe you can query for the activeEnergyBurned quantity type using the start and end date-time of the user workout in the predicate.

一杆小烟枪 2025-01-22 06:36:21

您需要按照以下代码片段将总卡路里信息添加到您的锻炼中

// create quantity type
let quantityType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!

//create unit
let unit = HKUnit.kilocalorie()

//create the quantity of total calories       
let quantity = HKQuantity(unit: unit, doubleValue: totalEnergyBurned)
        
//create a sample from the above data
let sample = HKCumulativeQuantitySample(type: quantityType,
                                        quantity: quantity,
                                        start: startDate,
                                        end: endDate)

//add the sample to your workout builder (HKWorkoutBuilder)
builder.add([sample], completion: { success, error in
            
})

You need to follow the below code snippet to add total calories information to your workout

// create quantity type
let quantityType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!

//create unit
let unit = HKUnit.kilocalorie()

//create the quantity of total calories       
let quantity = HKQuantity(unit: unit, doubleValue: totalEnergyBurned)
        
//create a sample from the above data
let sample = HKCumulativeQuantitySample(type: quantityType,
                                        quantity: quantity,
                                        start: startDate,
                                        end: endDate)

//add the sample to your workout builder (HKWorkoutBuilder)
builder.add([sample], completion: { success, error in
            
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文