Hey y’all, I have a code sample below. I wanted to know how I could change this into a SwiftUI view, just like text and all. Please help me!
My view controller:
import UIKit
import HealthKit
class ViewController: UIViewController {
let healthStore = HKHealthStore()
override func viewDidLoad() {
super.viewDidLoad()
authorizeHealthKit()
}
func authorizeHealthKit(){
let read = Set([HKObjectType.quantityType(forIdentifier: .heartRate)!])
let share = Set([HKObjectType.quantityType(forIdentifier: .heartRate)!])
healthStore.requestAuthorization(toShare: share, read: read) { (chk, error) in
if(chk){
print("permission granted")
self.latestHeartRate()
}
}
}
func latestHeartRate(){
guard let sampleType = HKObjectType.quantityType(forIdentifier: .heartRate) else{
return
}
let startDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictEndDate)
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sortDescriptor]) { (sample, result, error) in
guard error == nil else{
return
}
}
healthStore.execute(query)
}
}