![](/img/trans.png)
[英]Accessing Health Kit data into Apple Watch OS 2 excluding Workout Data
[英]Is there a way to read the Apple Watch exercise data with health kit?
健康套件中保存了活躍卡路里,停留時間和鍛煉,但似乎鍛煉數據僅存儲在活動應用程序中,而不存儲在healthkit中。 有沒有辦法訪問這些信息?
從iOS 9.3開始,您可以通過新的HKActivitySummaryQuery
讀取每個活動環,它將返回包含每個環的詳細信息的HKActivitySummary
對象。 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)
你會感興趣的部分是appleExerciseTime
的財產HKActivitySummary
。
請注意,此代碼不包括您需要進行的授權請求,以便能夠讀取HKObjectType.activitySummaryType()
的活動摘要。 無法將HKActivitySummary
寫入HKActivitySummary
,因此如果您請求寫入權限,應用程序將崩潰。
遺憾的是,綠色的運動環數據無法被應用訪問。 您應該向Apple提交雷達,要求它由HealthKit公開。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.