是否可以从 HealthKit 读取 Apple Watch 目标(移动、步进和站立)?
Posted
技术标签:
【中文标题】是否可以从 HealthKit 读取 Apple Watch 目标(移动、步进和站立)?【英文标题】:Is it possible to read Apple Watch goals (move, step and stand) from HealthKit? 【发布时间】:2016-11-29 09:03:35 【问题描述】:是否可以从 HealthKit 读取 Apple Watch 移动目标?
我可以使用数量标识符 HKQuantityTypeIdentifier.activeEnergyBurned 检索移动值。我找不到移动目标的类似标识符。
【问题讨论】:
【参考方案1】://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)
参考资料:
https://crunchybagel.com/accessing-activity-rings-data-from-healthkit https://developer.apple.com/documentation/healthkit/hkactivitysummary https://developer.apple.com/documentation/healthkit/hkactivitysummaryquery【讨论】:
谢谢。在使用以下方法启用健康包访问后,这对我来说非常有效:***.com/questions/37863093/…【参考方案2】:我得到了答案。移动目标可从HKActivitySummary
访问。
您应该请求读取 HKActivitySummaryType 的权限:
let activitySummaryType = HKActivitySummaryType.activitySummaryType()
let readDataTypes: Set<HKObjectType> = [activitySummaryType]
healthStore.requestAuthorization(toShare: nil, read: readDataTypes, completion: myCompletionHandler)
然后使用HKActivitySummaryQuery
阅读摘要信息
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)
可从 HKActivitySummary 访问的其他活动摘要数据可通过here 获取。
【讨论】:
您的示例不完整。我将您的代码复制到一个新项目中,但无法正常工作。以上是关于是否可以从 HealthKit 读取 Apple Watch 目标(移动、步进和站立)?的主要内容,如果未能解决你的问题,请参考以下文章
是否可以创建一个应用程序来读取 Apple Watch 的心率传感器?
从 Apple Watch 上的 HealthKit 中移除水样
从 Apple Watch 上的 HealthKit 中移除水样