From f8d27ae2097092488116b8903418adf12fcee40e Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Sat, 10 Feb 2024 00:53:50 -0700 Subject: initial commit --- iCUrHealth/ContentView.swift | 72 +++++++++++++++++++++++++++++++++++++- iCUrHealth/iCUrHealth.entitlements | 12 ++++--- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/iCUrHealth/ContentView.swift b/iCUrHealth/ContentView.swift index b68f101..cc8ea87 100644 --- a/iCUrHealth/ContentView.swift +++ b/iCUrHealth/ContentView.swift @@ -6,14 +6,84 @@ // import SwiftUI +import HealthKit +import HealthKitUI + +let allTypes: Set = [ + HKQuantityType.workoutType(), + HKQuantityType(.activeEnergyBurned), + HKQuantityType(.distanceCycling), + HKQuantityType(.distanceWalkingRunning), + HKQuantityType(.stepCount), + HKQuantityType(.heartRate), + HKCategoryType(.sleepChanges), + HKCategoryType(.sleepAnalysis) +] + +func stepCount(healthStore: HKHealthStore) async throws { + let stepType = HKQuantityType(.stepCount) + let descriptor = HKSampleQueryDescriptor(predicates:[.quantitySample(type: stepType)], sortDescriptors: [SortDescriptor(\.endDate, order: .reverse)], limit: 10) + let descriptor = HKSampleQuery( + + let results = try await descriptor.result(for: healthStore) + + for result in results { + print(result) + } +} struct ContentView: View { + @State var authenticated = false + @State var trigger = false + + let healthStore = HKHealthStore() + var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) - Text("Hello, world!") + Button(action: { + trigger.toggle() + }) { + Text("Force Permissions") + } + Button(action: { + if HKHealthStore.isHealthDataAvailable() { + print("YES!") + Task { + try await stepCount(healthStore: healthStore) + } + + + } else { + print("NOOOO!") + } + }) { + Text("Test Healthkit stuff") + }.disabled(!authenticated) + + .onAppear() { + + // Check that Health data is available on the device. + if HKHealthStore.isHealthDataAvailable() { + // Modifying the trigger initiates the health data + // access request. + trigger.toggle() + } + } + .healthDataAccessRequest(store: healthStore, + readTypes: allTypes, + trigger: trigger) { result in + switch result { + + case .success(_): + authenticated = true + case .failure(let error): + // Handle the error here. + fatalError("*** An error occurred while requesting authentication: \(error) ***") + } + } } .padding() } diff --git a/iCUrHealth/iCUrHealth.entitlements b/iCUrHealth/iCUrHealth.entitlements index f2ef3ae..ccc6b5e 100644 --- a/iCUrHealth/iCUrHealth.entitlements +++ b/iCUrHealth/iCUrHealth.entitlements @@ -2,9 +2,13 @@ - com.apple.security.app-sandbox - - com.apple.security.files.user-selected.read-only - + com.apple.developer.healthkit + + com.apple.developer.healthkit.access + + com.apple.security.app-sandbox + + com.apple.security.files.user-selected.read-only + -- cgit v1.2.3