iOS HealthKit 项目使用 HKActivitySummaryQuery - 摘要始终为零
iOS HealthKit Project using HKActivitySummaryQuery - Summaries Always Nil
Objective:从 HealthKit 中读取我的 Activity 数据(即 activity 环形数据 - 移动卡路里、锻炼分钟数和站立次数)
遇到的问题:结果处理程序returns 'nil' HKActivity摘要数组
我尝试过的:
我已将 HealthKit Capability 添加到项目中并添加了两个 info.plist 要求:
Privacy - Health Share Usage Description
Privacy - Health Update Usage Description
并确保在 iPhone.
中出现针对 HealthKit 的权限请求弹出窗口时,我点击了“全部允许”以获得权限
我也有很多周的 activity 数据记录,所以应该没问题。
如何检索 HKActivitySummary 对象数组,以便在我的 iOS 应用程序中复制环。
我的代码:
override func viewDidLoad() {
super.viewDidLoad()
startQueryForActivitySummary(view: self.view)
}
func startQueryForActivitySummary(view: UIView) {
let calendar = NSCalendar.current
let endDate = Date()
guard let startDate = calendar.date(byAdding: .day, value: -7, to: endDate) else {
fatalError("*** Unable to create the start date ***")
}
let units: Set<Calendar.Component> = [.day, .month, .year, .era]
var startDateComponents = calendar.dateComponents(units, from: startDate)
startDateComponents.calendar = calendar
var endDateComponents = calendar.dateComponents(units, from: endDate)
endDateComponents.calendar = calendar
let queryPredicate = HKQuery.predicate(forActivitySummariesBetweenStart: startDateComponents,
end: endDateComponents)
let query = HKActivitySummaryQuery(predicate: queryPredicate) { (query, summaries, error) -> Void in
if let summaries = summaries { // print(summaries) before this line will always return nil.
if let summary = summaries.first {
let activeEnergyBurned = summary.activeEnergyBurned.doubleValue(for: HKUnit.kilocalorie())
let activeEnergyBurnedGoal = summary.activeEnergyBurnedGoal.doubleValue(for: HKUnit.kilocalorie())
let activeEnergyBurnGoalPercent = round(activeEnergyBurned/activeEnergyBurnedGoal)
let frame = CGRect(x: 0, y: 0, width: 200, height: 200)
let ringView = HKActivityRingView(frame: frame)
view.addSubview(ringView)
ringView.setActivitySummary(summary, animated: true)
}
}
}
let allTypes = Set([HKObjectType.workoutType(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!])
let healthStore = HKHealthStore()
healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
healthStore.execute(query)
}
}
我认为您缺少权限 HKObjectType.activitySummaryType()
像这样扩展你的类型:
let allTypes = Set([
HKObjectType.workoutType(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKObjectType.activitySummaryType()
])
这是文档:https://developer.apple.com/documentation/healthkit/hkobjecttype/1615319-activitysummarytype
Objective:从 HealthKit 中读取我的 Activity 数据(即 activity 环形数据 - 移动卡路里、锻炼分钟数和站立次数)
遇到的问题:结果处理程序returns 'nil' HKActivity摘要数组
我尝试过的:
我已将 HealthKit Capability 添加到项目中并添加了两个 info.plist 要求:
Privacy - Health Share Usage Description
Privacy - Health Update Usage Description
并确保在 iPhone.
中出现针对 HealthKit 的权限请求弹出窗口时,我点击了“全部允许”以获得权限我也有很多周的 activity 数据记录,所以应该没问题。
如何检索 HKActivitySummary 对象数组,以便在我的 iOS 应用程序中复制环。
我的代码:
override func viewDidLoad() {
super.viewDidLoad()
startQueryForActivitySummary(view: self.view)
}
func startQueryForActivitySummary(view: UIView) {
let calendar = NSCalendar.current
let endDate = Date()
guard let startDate = calendar.date(byAdding: .day, value: -7, to: endDate) else {
fatalError("*** Unable to create the start date ***")
}
let units: Set<Calendar.Component> = [.day, .month, .year, .era]
var startDateComponents = calendar.dateComponents(units, from: startDate)
startDateComponents.calendar = calendar
var endDateComponents = calendar.dateComponents(units, from: endDate)
endDateComponents.calendar = calendar
let queryPredicate = HKQuery.predicate(forActivitySummariesBetweenStart: startDateComponents,
end: endDateComponents)
let query = HKActivitySummaryQuery(predicate: queryPredicate) { (query, summaries, error) -> Void in
if let summaries = summaries { // print(summaries) before this line will always return nil.
if let summary = summaries.first {
let activeEnergyBurned = summary.activeEnergyBurned.doubleValue(for: HKUnit.kilocalorie())
let activeEnergyBurnedGoal = summary.activeEnergyBurnedGoal.doubleValue(for: HKUnit.kilocalorie())
let activeEnergyBurnGoalPercent = round(activeEnergyBurned/activeEnergyBurnedGoal)
let frame = CGRect(x: 0, y: 0, width: 200, height: 200)
let ringView = HKActivityRingView(frame: frame)
view.addSubview(ringView)
ringView.setActivitySummary(summary, animated: true)
}
}
}
let allTypes = Set([HKObjectType.workoutType(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!])
let healthStore = HKHealthStore()
healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
healthStore.execute(query)
}
}
我认为您缺少权限 HKObjectType.activitySummaryType()
像这样扩展你的类型:
let allTypes = Set([
HKObjectType.workoutType(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKObjectType.activitySummaryType()
])
这是文档:https://developer.apple.com/documentation/healthkit/hkobjecttype/1615319-activitysummarytype