diff options
-rw-r--r-- | iCUrHealth/ContentView.swift | 72 | ||||
-rw-r--r-- | 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 @@ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> - <key>com.apple.security.app-sandbox</key> - <true/> - <key>com.apple.security.files.user-selected.read-only</key> - <true/> + <key>com.apple.developer.healthkit</key> + <true/> + <key>com.apple.developer.healthkit.access</key> + <array/> + <key>com.apple.security.app-sandbox</key> + <true/> + <key>com.apple.security.files.user-selected.read-only</key> + <true/> </dict> </plist> |