我正在尝试在 iWatch 中获取 bodyTemperature,所以下面是我编写的代码
override func willActivate() {
super.willActivate()
guard let type=HKQuantityType.quantityType(forIdentifier: .bodyTemperature) else {
return
}
let dataTypes=Set(arrayLiteral:type)
healthStore.requestAuthorization(toShare: nil, read: dataTypes) { (success, error) in
if success==false{
print("Authorize Unsuccessfull ")
}
}
}
@IBAction func startBtnTapped(){
startSession()
}
func startSession(){
session = HKWorkoutSession(activityType: HKWorkoutActivityType.crossTraining, locationType: HKWorkoutSessionLocationType.indoor)
session?.delegate = self
healthStore.start(session!)
}
func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date) {
switch toState {
case .running:
if let query=createQuery(date){
healthStore.execute(query)
}
case .ended:
workoutEnd(date)
default:
print("Unexpted state:\(toState)")
}
}
func createQuery(_ startDate:Date)->HKQuery?{
guard let type=HKObjectType.quantityType(forIdentifier: .bodyTemperature) else {
return nil
}
let predicate=getPredicate()
let query=HKAnchoredObjectQuery(type: type, predicate: predicate, anchor: nil, limit: Int(HKObjectQueryNoLimit)) { (query, sample, deletedObject, newAnchor, error) in
print("query:\(query)")
print("sample:\(sample)")
print("deletedObject:\(deletedObject)")
print("newAnchor:\(newAnchor)")
self.updateValues(sample)
}
query.updateHandler={(query,sample,deletedObject,newAcnhor,error) in
self.updateValues(sample)
}
return query
}
在每次完成块重新运行 0 值时执行查询。相同的代码正在获取心率。我不知道如何从halthkit 中获取其他信息,如体温、葡萄糖率、bodyFatPercentage、bodyMassIndex 等。任何人都可以帮助我吗
提前致谢