Skip to content

Instantly share code, notes, and snippets.

@KrishanMadushanka
Created December 30, 2022 14:58
Show Gist options
  • Select an option

  • Save KrishanMadushanka/9b13c7cb4352b8fa810f628927603acf to your computer and use it in GitHub Desktop.

Select an option

Save KrishanMadushanka/9b13c7cb4352b8fa810f628927603acf to your computer and use it in GitHub Desktop.
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