Learn Courses My Dashboard

How do I change this UI Kit to Swift UI?

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() {
    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
                print("permission granted")
    func latestHeartRate(){
       guard let sampleType = HKObjectType.quantityType(forIdentifier: .heartRate) else{
        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{

Use all the same functions

Make a SwiftUI view, instead of viewDidLoad, you’ll add an onAppear modifier to the view.

For let HealthStore Make sure to add @ State to the variable

Thanks @mikaelacaron, I followed what you said, and it went in the console, but can I make the heart rate for the past week in a list view?

You can. You would need some model and fetch the data in onAppear