Ios 有没有办法通过健康工具包读取Apple Watch运动数据?

Ios 有没有办法通过健康工具包读取Apple Watch运动数据?,ios,apple-watch,healthkit,Ios,Apple Watch,Healthkit,活动卡路里、站立时间和训练都保存在healthkit中,但似乎运动数据只存储在活动应用程序中,而不存储在healthkit中。有没有办法访问这些信息?不幸的是,应用程序无法访问绿色运动戒指数据。您应该向苹果公司提交雷达文件,要求HealthKit将其曝光。从iOS 9.3开始,您可以通过新的HKActivitySummary查询读取每个活动环,该查询将返回包含每个环详细信息的HKActivitySummary对象。详情如下: // Create the date components for t

活动卡路里、站立时间和训练都保存在healthkit中,但似乎运动数据只存储在活动应用程序中,而不存储在healthkit中。有没有办法访问这些信息?

不幸的是,应用程序无法访问绿色运动戒指数据。您应该向苹果公司提交雷达文件,要求HealthKit将其曝光。

从iOS 9.3开始,您可以通过新的
HKActivitySummary查询
读取每个活动环,该查询将返回包含每个环详细信息的
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)
您感兴趣的是
HKActivitySummary
appleExerciseTime
属性


请注意,此代码不包括您需要发出的授权请求,以便能够阅读属于
HKObjectType.activitySummaryType()
的活动摘要。无法将
HKActivitySummary
写入HealthKit,因此如果您请求写入权限,应用程序将崩溃。

从iOS 9.3开始,运动戒指数据现在可用(请参阅我的答案)