gpt4 book ai didi

ios - 如何检索healthkit数据并显示?

转载 作者:行者123 更新时间:2023-11-28 09:33:58 24 4
gpt4 key购买 nike

我正在开发一个步骤应用程序。我想从健康应用程序中检索 Healthkit 数据,但我不知道该怎么做。我在互联网上找不到任何东西。我想从健康应用程序中获取步数数据。

最佳答案

For Asking permission 请求许可

if HKHealthStore.isHealthDataAvailable() {
var writeDataTypes: Set<AnyHashable> = self.dataTypesToWrite()
var readDataTypes: Set<AnyHashable> = self.dataTypesToRead()
self.healthStore.requestAuthorization(toShareTypes: writeDataTypes, readTypes: readDataTypes, completion: {(_ success: Bool, _ error: Error) -> Void in
if !success {
print("You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: \(error). If you're using a simulator, try it on a device.")
return
}
})
}

写入数据

func dataTypesToWrite() -> Set<AnyHashable> {
var heightType: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .height)
var weightType: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .bodyMass)
var systolic: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .bloodPressureSystolic)
var dystolic: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .bloodPressureDiastolic)
return Set<AnyHashable>([heightType, weightType, systolic, dystolic])
}

从health kit读取数据

func dataTypesToRead() -> Set<AnyHashable> {
var heightType: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .height)
var weightType: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .bodyMass)
var systolic: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .bloodPressureSystolic)
var dystolic: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .bloodPressureDiastolic)
var sleepAnalysis: HKCategoryType? = HKObjectType.categoryType(forIdentifier: .sleepAnalysis)
var step: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .stepCount)
var walking: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)
var cycling: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .distanceCycling)
var basalEnergyBurned: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .basalEnergyBurned)

如果你想获得上周的步数,那么你可以按照下面的代码

self.healthStore = HKHealthStore()
var calendar = Calendar.current
var interval = DateComponents()
interval.day = 1
var anchorComponents: DateComponents? = calendar.dateComponents([.day, .month, .year], from: Date())
anchorComponents?.hour = 0
var anchorDate: Date? = calendar.date(fromComponents: anchorComponents)
var quantityType: HKQuantityType? = HKObjectType.quantityType(forIdentifier: .stepCount)
// Create the query
var query = HKStatisticsCollectionQuery(quantityType, quantitySamplePredicate: nil, options: HKStatisticsOptionCumulativeSum, anchorDate: anchorDate, intervalComponents: interval)
// Set the results handler
query.initialResultsHandler = {(_ query: HKStatisticsCollectionQuery, _ results: HKStatisticsCollection, _ error: Error) -> Void in
if error != nil {
// Perform proper error handling here
print("*** An error occurred while calculating the statistics: \(error?.localizedDescription) ***")
}
var endDate = Date()
var startDate: Date? = calendar.date(byAddingUnit: .day, value: -7, to: endDate, options: 0)
// Plot the daily step counts over the past 7 days
results.enumerateStatistics(from: startDate, to: endDate, block: {(_ result: HKStatistics, _ stop: Bool) -> Void in
var quantity: HKQuantity? = result.sumQuantity()
if quantity != nil {
var date: Date? = result.startDate
var value: Double? = quantity?.doubleValue(forUnit: HKUnit.count())
totalStepsCount = String(format: "%.f", value)
DispatchQueue.main.async(execute: {() -> Void in
self.calculateStepCountAndShow()
})
print("\(date): \(value)")
}
})
}
self.healthStore.executeQuery(query)
}

关于ios - 如何检索healthkit数据并显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42503863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com