gpt4 book ai didi

ios - 使用 HealthKit 结果填充 TableView - Swift

转载 作者:可可西里 更新时间:2023-11-01 01:50:20 24 4
gpt4 key购买 nike

我正在尝试用 Apple Health 返回的 HealthKit 来填充我的 TableView 。我目前从 Apple Health 获取过去 7 天的日期和心率。我可以在控制台中清楚地看到所有内容。

这是我的 block 代码:

    func getTodaysHeartRate(completion: (@escaping (HKQuantitySample) -> Void)) {
print("func")
let heartRateUnit:HKUnit = HKUnit(from: "count/min")
let heartRateType:HKQuantityType = HKQuantityType.quantityType(forIdentifier: .heartRate)!


let startDate = Date() - 7 * 24 * 60 * 60
let endDate = Date()
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])


let sortDescriptors = [
NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
]

let heartRateQuery = HKSampleQuery(sampleType: heartRateType,
predicate: predicate,
limit: Int(HKObjectQueryNoLimit),
sortDescriptors: sortDescriptors)
{ (query:HKSampleQuery, results:[HKSample]?, error:Error?) -> Void in

guard error == nil else { print("error"); return }
//print("results")
//print(results!)

for result in results! {
guard let currData:HKQuantitySample = result as? HKQuantitySample else { return }

print("Heart Rate: \(currData.quantity.doubleValue(for: heartRateUnit))")
print("Times: \(currData.startDate)")
//print("End Date: \(currData.endDate)")
//print("quantityType: \(currData.quantityType)")
//print("Metadata: \(String(describing: currData.metadata))")
//print("UUID: \(currData.uuid)")
//print("Source: \(currData.sourceRevision)")
//print("Device: \(String(describing: currData.device))")
//print("---------------------------------\n")
}

}
healthStore.execute(heartRateQuery)

}

@IBOutlet weak var heartRate: UILabel!
@IBAction func getHeartRate(_ sender: Any) {
getTodaysHeartRate { (result) in
print("\(result)")
DispatchQueue.main.async {
self.heartRate.text = "\(result)"
}
}

}
@IBAction func emailResult(_ sender: Any) {
self.sendEmail()
}

最佳答案

我在这里为您创建了示例项目,我对您的代码进行了一些更改,如下所示,并为此添加了注释:

import UIKit
import HealthKit

class ViewController: UIViewController {

//add a tableview to your storyboard and connect delegate and datasource
@IBOutlet weak var tblHeartRateData: UITableView!

let healthStore = HKHealthStore()

//create an array of HKSample which will hold your data from healthkit or you can create a custom class for that
var heartRateData: [HKSample]?

override func viewDidLoad() {
super.viewDidLoad()

//Get data access permission
getHealthKitPermission()
}

@IBAction func getHeartRate(_ sender: Any) {

getTodaysHeartRate { (result) in
DispatchQueue.main.async {

//Here you will get your healthkit data.
self.heartRateData = result
//Once you get it reload your table view
self.tblHeartRateData.reloadData()
}
}
}

//Permission
func getHealthKitPermission() {
let healthkitTypesToRead = NSSet(array: [
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate) ?? ""
])

healthStore.requestAuthorization(toShare: nil, read: healthkitTypesToRead as? Set) { (success, error) in
if success {
print("Permission accept.")

} else {
if error != nil {
print(error ?? "")
}
print("Permission denied.")
}
}
}

func getTodaysHeartRate(completion: (@escaping ([HKSample]) -> Void)) {
print("func")

let heartRateType:HKQuantityType = HKQuantityType.quantityType(forIdentifier: .heartRate)!

//predicate
let startDate = Date() - 2 * 24 * 60 * 60
let endDate = Date()
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])

//descriptor
let sortDescriptors = [
NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
]

let heartRateQuery = HKSampleQuery(sampleType: heartRateType,
predicate: predicate,
limit: Int(HKObjectQueryNoLimit),
sortDescriptors: sortDescriptors)
{ (query:HKSampleQuery, results:[HKSample]?, error:Error?) -> Void in

guard error == nil else { print("error"); return }

//Here I have added completion which will pass data when button will tap.
completion(results!)

}
healthStore.execute(heartRateQuery)

}
}

//TableView Methods.
extension ViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return heartRateData?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HeartDataTableViewCell

let heartRateUnit:HKUnit = HKUnit(from: "count/min")
let heartData = self.heartRateData?[indexPath.row]
guard let currData:HKQuantitySample = heartData as? HKQuantitySample else { return UITableViewCell()}

cell.lblHeartRate.text = "Heart Rate: \(currData.quantity.doubleValue(for: heartRateUnit))"

return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 106
}
}

有关更多信息,请查看我们的 THIS演示项目。

你的结果将是:

enter image description here

关于ios - 使用 HealthKit 结果填充 TableView - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54848420/

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