活跃卡路里,停留时间和锻炼都保存在healthkit中,但似乎锻炼数据仅存储在Activity应用程序中,但不存储在healthkit中。有没有办法访问这些信息?
答案 0 :(得分:5)
从iOS 9.3开始,您可以通过新的JSON.parse
读取每个活动响铃,这将返回包含每个响铃详细信息的HKActivitySummaryQuery
对象。 sample code from Apple如下:
HKActivitySummary
您感兴趣的作品是// 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)
的{{1}}属性。
请注意,此代码不包含您需要进行的授权请求,以便能够阅读appleExerciseTime
的活动摘要。无法将HKActivitySummary
写入HealthKit,因此如果您请求写入权限,应用程序将崩溃。
答案 1 :(得分:0)
很遗憾,绿色的运动环数据无法被应用访问。您应该向Apple提交雷达,要求它由HealthKit公开。