ios - iOS Swift,如何区分使用 Apple Watch 和 iPhone 的步骤?
问题描述
我正在使用 HealthKit 框架从使用 HKSource 的用户那里检索步骤。在 Xcode Objective C 中,我使用 Bundle Identifier 来区分来自 watch/iPhone 的步骤。但是使用 Swift 我无法做到这一点。请建议。
提前致谢。
解决方案
.separateBySource
在查询的选项中使用。例如:
guard let sampleType = HKObjectType.quantityType(forIdentifier: .stepCount)
else {
fatalError("Couldn't create the quantityType for .stepCount")
}
let calendar = Calendar.current
var dateComponents = DateComponents()
dateComponents.day = 1
var anchorComponents = calendar.dateComponents([.day, .month, .year], from: Date())
anchorComponents.hour = 0
guard let anchorDate = calendar.date(from: anchorComponents) else {
fatalError("Couldn't get the anchor date")
}
let stepsCumulativeQuery =
HKStatisticsCollectionQuery(quantityType: sampleType,
quantitySamplePredicate: nil,
options: [.cumulativeSum , .separateBySource],
anchorDate: anchorDate,
intervalComponents: dateComponents)
推荐阅读
- java - 如何从android将新节点放入firebase
- vim - Vim - 当 foucs 从 vim/nvim 丢失时保存缓冲区。与 VSCode 的 "files.autoSave": "onFocusChange" 设置相同
- npm - npmrc - 冻结锁定文件?
- python - NumPy 如何将复数矩阵相乘?
- java - Java 新手,需要帮助将文本字段中的元素存储到自定义类型对象中并将对象存储到 ArrayList 中
- javascript - 尝试根据带有按钮的标题/描述按字母顺序对挑战进行排序
- php - 在获取的 URL 中查找单词时 PHP PREG 匹配错误
- c++ - 有没有办法可以禁用 CodeSonar 生成的特定行上的警告
- snmp - 如何使用自定义 Bash 脚本、自定义企业 OID 和自定义 MIB 扩展 SNMP 代理
- python - Azure ML 探查器在哪里运行?