我正在开发一个IOS睡眠应用程序,在那里我需要做睡眠分析。我正在使用Healthkit来获取睡眠数据,在那里我可以使用以下代码成功地获取睡眠分析数据:
func retrieveSleepAnalysis(from startDate: Date?, to endDate: Date? , completion: @escaping ([HKCategorySample], Error?) -> Void) {
guard let sleepType = HKObjectType.categoryType(forIdentifier: .sleepAnalysis) else { return}
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
let query = HKSampleQuery(sampleType: sleepType, predicate: predicate, limit: 10000, sortDescriptors: [sortDescriptor]) { (query, result, error) in
if error != nil {
completion([], error)
return
}
if let result = result {
let samples = result.compactMap({ $0 as? HKCategorySample})
completion(samples, nil)
}
}
// finally, we execute our query
HKHealthStore().execute(query)
}我无法找到任何用于睡眠快速眼动周期、深度睡眠、轻度睡眠等的healthKit代码,甚至可以从healthKit获得这些数据吗?如果是,怎么做?如果不是用healthKit,如何在IOS应用程序中做到这一点?
发布于 2022-06-10 15:50:28
你可能会在今年的"HealthKit的新特点是什么?“论坛上找到你想要的东西
下面是如何为所有示例声明谓词:
// Predicate for all asleep samples (unspecified, core, deep, REM)
let allAsleepPredicate = HKCategoryValueSleepAnalysis.predicateForSamples(equalTo: .allAsleepValues)下面是如何使用它获取范围内的所有样本:
let healthStore = HKHealthStore()
let sleepType = HKCategoryType(.sleepAnalysis)
let dateRangePredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [dateRangePredicate, allAsleepPredicate])
let query = HKSampleQuery(sampleType: sleepType, predicate: predicate) { (query, result, error) in
// handle results
}希望这有帮助
https://stackoverflow.com/questions/72570062
复制相似问题