繁体   English   中英

有没有办法用健康套件阅读Apple Watch运动数据?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM