WWDC 2017 で、Apple は新しい量のサンプルとしてvo2Maxの追加を発表しました。vo2Max サンプルを返すためには、どの変数を配置する必要がありますか? 健康アプリによると、Vo2Max の下に「Apple Watchは、持続的な心拍数測定で少なくとも 20 分間、屋外での激しいウォーキングやワークアウト アプリでのランニングを行ったときに、予測された vo2Max を記録します。」サードパーティのアプリでは利用できないように聞こえますが、WWDC 2017 の 3:20 のこのクリップでは、利用できるはずだと思われているのはどれですか?
.running 屋外ワークアウト中の以下のコードは、20 分後でも空の配列を返します。
func startVO2MaxQuery(from startDate: Date, updateHandler: @escaping ([HKQuantitySample]) -> Void) {
let typeIdentifier = HKQuantityTypeIdentifier.vo2Max
startQuery(ofType: typeIdentifier, from: startDate) { _, samples, _, _, error in
guard let quantitySamples = samples as? [HKQuantitySample] else {
print("Heart rate query failed with error: \(String(describing: error))")
return
}
print("VO2Max samples = \(quantitySamples)")
updateHandler(quantitySamples)
}
}
private func startQuery(ofType type: HKQuantityTypeIdentifier, from startDate: Date, handler: @escaping
(HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void) {
let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: .strictStartDate)
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate, devicePredicate])
let quantityType = HKObjectType.quantityType(forIdentifier: type)!
let query = HKAnchoredObjectQuery(type: quantityType, predicate: queryPredicate, anchor: nil,
limit: HKObjectQueryNoLimit, resultsHandler: handler)
query.updateHandler = handler
healthStore.execute(query)
activeDataQueries.append(query)
}
パーミッション
private func requestAccessToHealthKit() {
let healthStore = HKHealthStore()
let allTypes = Set([HKObjectType.workoutType(),
HKSeriesType.workoutRoute(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKObjectType.quantityType(forIdentifier: .heartRate)!,
HKObjectType.quantityType(forIdentifier: .vo2Max)!,
HKObjectType.quantityType(forIdentifier: .stepCount)!,
HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!])
healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
if !success {
print("failed HealthKit Authorization from iPhone \(String(describing: error?.localizedDescription))")
}
print("Successful HealthKit Authorization directly from the watch")
}
}
他に何かしなければならないことはありますか?Apple のドキュメントはかなりまばらです。