我试图从Healthkit中获取并更新高度,但没有获得任何快速文档3.如何在swift 3中获取并更新Healthkit的高度?
有没有什么办法从healthKit获取高度还可以更新healthKit中的高度?
我试图从Healthkit中获取并更新高度,但没有获得任何快速文档3.如何在swift 3中获取并更新Healthkit的高度?
有没有什么办法从healthKit获取高度还可以更新healthKit中的高度?
创建healthkit实例
let healthKitStore:HKHealthStore = HKHealthStore()
请求允许
func requestPermissions(completion: @escaping ((_ success:Bool, _ error:Error?) -> Void)){
let healthKitTypesToRead : Set<HKSampleType> = [
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.height)!,
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!,
]
let healthKitTypesToWrite: Set<HKSampleType> = [
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.height)!,
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!,
]
if !HKHealthStore.isHealthDataAvailable() {
print("Error: HealthKit is not available in this Device")
completion(false, error)
return
}
healthKitStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success, error) -> Void in
completion(success,error)
}
}
读取高度
let heightType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.height)!
let query = HKSampleQuery(sampleType: heightType, predicate: nil, limit: 1, sortDescriptors: nil) { (query, results, error) in
if let result = results?.first as? HKQuantitySample{
print("Height => \(result.quantity)")
}else{
print("OOPS didnt get height \nResults => \(results), error => \(error)")
}
}
self.healthKitStore.execute(query)
写作身高
let height = YOUR_HEIGHT
if let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.height) {
let date = Date()
let quantity = HKQuantity(unit: HKUnit.inch(), doubleValue: height!)
let sample = HKQuantitySample(type: type, quantity: quantity, start: date, end: date)
self.healthKitStore.save(sample, withCompletion: { (success, error) in
print("Saved \(success), error \(error)")
})
}
现在,如果你想读,写权重,然后只需用
HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)
注意:不要忘了添加这些权限
隐私 - 健康更新使用说明
隐私 - 健康分享使用说明
希望这对其他仍在寻找它的人有帮助
我已经在iPhone的默认应用程序运行状态中添加了高度。但是我想从运行状况高度到我的应用程序,我得到零值。你能给我一个方向吗? –
@HimaliShah:我已经提到了阅读代码,但不能帮助,如果我不能看到的代码,发表一个问题,你的代码写的检索它 –
您是否找到解决方案? – guru
我也有同样的问题 – guru