Created
December 30, 2022 14:58
-
-
Save KrishanMadushanka/9b13c7cb4352b8fa810f628927603acf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| import HealthKit | |
| @main | |
| struct HealthkitIntegrationApp: App { | |
| private let healthStore: HKHealthStore | |
| init() { | |
| guard HKHealthStore.isHealthDataAvailable() else { fatalError("This app requires a device that supports HealthKit") } | |
| healthStore = HKHealthStore() | |
| requestHealthkitPermissions() | |
| } | |
| private func requestHealthkitPermissions() { | |
| let sampleTypesToRead = Set([ | |
| HKObjectType.quantityType(forIdentifier: .heartRate)!, | |
| HKObjectType.quantityType(forIdentifier: .stepCount)!, | |
| HKObjectType.categoryType(forIdentifier: .sleepAnalysis)!, | |
| ]) | |
| healthStore.requestAuthorization(toShare: nil, read: sampleTypesToRead) { (success, error) in | |
| print("Request Authorization -- Success: ", success, " Error: ", error ?? "nil") | |
| } | |
| } | |
| var body: some Scene { | |
| WindowGroup { | |
| ContentView().environmentObject(healthStore) | |
| } | |
| } | |
| } | |
| extension HKHealthStore: ObservableObject{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment