gpt4 book ai didi

ios - 有没有办法用健康包读取Apple Watch运动数据?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:17 24 4
gpt4 key购买 nike

healthkit 中保存了事件卡路里、站立时间和锻炼,但运动数据似乎只存储在 Activity 应用程序中,而没有存储在 healthkit 中。有什么方法可以访问此信息?

最佳答案

从 iOS 9.3 开始,您可以通过新的 HKActivitySummaryQuery 读取每个事件圆环,它将返回一个包含每个圆环详细信息的 HKActivitySummary 对象。 sample code from Apple如下:

// Create the date components for the predicate
guard let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) else {
fatalError("*** This should never fail. ***")
}

let endDate = NSDate()

guard let startDate = calendar.dateByAddingUnit(.Day, value: -7, toDate: endDate, options: []) else {
fatalError("*** unable to calculate the start date ***")
}

let startDateComponents = calendar.components(units, fromDate: startDate)
startDateComponents.calendar = calendar

let endDateComponents = calendar.components(units, fromDate:endDate)
endDateComponents.calendar = calendar

let startDateComponents = calendar.components(units, fromDate: startDate)


// Create the predicate for the query
let summariesWithinRange = HKQuery.predicateForActivitySummariesBetweenStartDateComponents(startDateComponents, endDateComponents: endDateComponents)

// Build the query
let query = HKActivitySummaryQuery(predicate: summariesWithinRange) { (query, summaries, error) -> Void in
guard let activitySummaries = summaries else {
guard let queryError = error else {
fatalError("*** Did not return a valid error object. ***")
}

// Handle the error here...

return
}

// Do something with the summaries here...
if let summary = summaries?.first {
NSLog("Exercise: \(summary.appleExerciseTime)")
}
}

// Run the query
store.executeQuery(query)

您感兴趣的部分是 HKActivitySummaryappleExerciseTime 属性。

请注意,此代码不包括您需要进行的授权请求才能阅读HKObjectType.activitySummaryType() 的事件摘要。无法将 HKActivitySummary 写入 HealthKit,因此如果您请求写入权限,应用程序将崩溃。

关于ios - 有没有办法用健康包读取Apple Watch运动数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32533170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com