ios - SwiftUI 读取 HealthKit 数据
问题描述
我正在尝试从 SwiftUI 中的 HealthKit 读取 HealthKit 数据。如何访问一天内燃烧的卡路里、心率或步行距离等变量?我发布了我在下面编写的代码。例如,我想将变量放在 TextField 中。
var healthStore = HKHealthStore()
func autorizeHealthKit() {
// Used to define the identifiers that create quantity type objects.
let allTypes = Set([HKObjectType.workoutType(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKObjectType.quantityType(forIdentifier: .distanceCycling)!,
HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!,
HKObjectType.quantityType(forIdentifier: .heartRate)!])
// Requests permission to save and read the specified data types.
healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { _, _ in }
}
var body: some View {
HStack(spacing: 30) {
ZStack {
Color.gray.edgesIgnoringSafeArea(.all)
Text("HeartRate")
.bold()
}.frame(width: 140, height: 80)
ZStack {
Color.gray.edgesIgnoringSafeArea(.all)
Text("Calories")
.bold()
}.frame(width: 140, height: 80)
}.onAppear(perform: autorizeHealthKit)
}
}
解决方案
推荐阅读
- html - Bootstrap 类名顺序命名约定
- ruby - 比较 ruby 中的 DateTimes,你能回答这个难题吗?
- ios - UITableView commitEditingStyle:用于展开和折叠部分
- html - Django中并排的导航栏?
- javascript - 如何设置自动播放的轮播中使用的数组的当前索引的状态,以作为道具传递给另一个组件?
- angular - 从 ng serve 中排除文件夹
- laravel - 来自数据库 Composer 问题的 laravel 环境
- apache-spark - ProcessingTimeExecutor:66 - 当前批次落后。触发间隔为xxx毫秒,但花费了xxx毫秒
- scala - 线程“主”java.lang.NoClassDefFoundError 中的异常:scala/Cloneable
- laravel - 我想通过关系显示每个技术 ID 的信息