简体   繁体   English

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

[英]Is there a way to read the Apple Watch exercise data with health kit?

Active calories, stand hours and workouts are saved in healthkit, but it seems that exercise data is stored only in Activity app but not in healthkit. 健康套件中保存了活跃卡路里,停留时间和锻炼,但似乎锻炼数据仅存储在活动应用程序中,而不存储在healthkit中。 Are there any way to access this information? 有没有办法访问这些信息?

As of iOS 9.3, you can read each of the activity rings via the new HKActivitySummaryQuery which will return an HKActivitySummary object containing details of each ring. 从iOS 9.3开始,您可以通过新的HKActivitySummaryQuery读取每个活动环,它将返回包含每个环的详细信息的HKActivitySummary对象。 The sample code from Apple is as follows: 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)

The piece you'll be interested in is the appleExerciseTime property of the HKActivitySummary . 你会感兴趣的部分是appleExerciseTime的财产HKActivitySummary

Please note that this code doesn't include the authorisation request you'll need to make to be able to read the activity summary which is of HKObjectType.activitySummaryType() . 请注意,此代码不包括您需要进行的授权请求,以便能够读取HKObjectType.activitySummaryType()的活动摘要。 It is not possible to write HKActivitySummary to HealthKit so the app will crash if you request write permissions. 无法将HKActivitySummary写入HKActivitySummary ,因此如果您请求写入权限,应用程序将崩溃。

The green Exercise ring data is unfortunately not accessible to apps. 遗憾的是,绿色的运动环数据无法被应用访问。 You should file a radar with Apple to ask for it to be exposed by HealthKit. 您应该向Apple提交雷达,要求它由HealthKit公开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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