let healthStore = HKHealthStore()
let sixMinuteWalkType = HKSampleType.quantityType(forIdentifier: .sixMinuteWalkTestDistance)!
if sixMinuteWalkType.allowsRecalibrationForEstimates {
healthStore.recalibrateEstimates(sampleType: sixMinuteWalkType, date: surgeryDate) {
(success, error) in
0:03 - Get authorized for walkingSteadiness type
let types: Set = [
HKObjectType.quantityType(forIdentifier: .walkingSteadiness)!
healthKitStore.requestAuthorization(toShare: nil, read: types) { _, _ in }
0:04 - Construct a query for most recent walkingSteadiness score
let steadinessType = HKObjectType.quantityType(forIdentifier: .walkingSteadiness)
let sortByEndDate = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
let query = HKSampleQuery(sampleType: steadinessType,
predicate: nil,
limit: 1,
sortDescriptors: [sortByEndDate]) { (query, samples, error) in
if let sample = samples?.first as? HKQuantitySample{
let recentScore = sample.quantity.doubleValue(forUnit: .percentUnit)
updateStatus(score: recentScore)
self.healthStore.execute(query)
0:05 - Construct a query for most recent walkingSteadiness classification
let steadinessType = HKObjectType.quantityType(forIdentifier: .walkingSteadiness)
let query = HKSampleQuery(sampleType: steadinessType,
predicate: nil,
limit: 1,
sortDescriptors: nil) { (query, samples, error) in
if let sample = samples?.first as? HKQuantitySample{
let recentScore = sample.quantity.doubleValue(forUnit: .percentUnit)
let recentClassification = HKAppleWalkingSteadinessClassification(for: walkingSteadiness.quantity)
updateStatus(classification: recentClassification, score: recentScore)
self.healthStore.execute(query)
0:06 - Get authorized .walkingSteadinessEvent
let types: Set = [
HKObjectType.categoryType(forIdentifier: .walkingSteadinessEvent)!
healthKitStore.requestAuthorization(toShare: nil, read: types) { _, _ in }
0:07 - Watch for walkingSteadiness notifications
let notificationType = HKCategoryType.categoryType(forIdentifier: .appleWalkingSteadinessEvent)!
let query = HKObserverQuery(sampleType: notificationType, predicate: nil) {
(query, completionHandler, errorOrNil) in
if let error = errorOrNil {
return
promptCheckupForNotification()
completionHandler()
self.healthStore.execute(query)
0:08 - Query walking steadiness in the past 6 weeks
let end = Date()
let start = Calendar.current.date(byAdding: .week, value: -6, to: end)
let datePredicate = HKQuery.predicateForSamples(withStart: start, end: end, options: [])
let steadinessType = HKObjectType.quantityType(forIdentifier: .walkingSteadiness)
let sortByEndDate = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
let query = HKSampleQuery(sampleType: steadinessType,
predicate: sortByEndDate,
limit: nil,
sortDescriptors:[sortByEndDate]) { (_, samples, _) in
detectTrends(samples)
self.healthStore.execute(query)
Apple Developer Program
Apple Developer Enterprise Program
App Store Small Business Program
MFi Program (英文)
News Partner Program (英文)
Video Partner Program (英文)
安全赏金计划 (英文)
Security Research Device Program (英文)