我想在iWatch中获取体温,所以下面是我编写的代码
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那里得到其他东西的程序,比如体温,葡萄糖率,体脂百分比,体脂指数等等。有人能帮我吗
提前谢谢
最佳答案
苹果手表上没有体温传感器。HealthKit中的任何体温样本都将由应用程序保存或由用户手动输入。
关于ios - 如何使用iOS中的health-kit获得体温?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41011822/