diff options
author | Navan Chauhan <navanchauhan@gmail.com> | 2024-02-10 00:53:50 -0700 |
---|---|---|
committer | Navan Chauhan <navanchauhan@gmail.com> | 2024-02-10 00:53:50 -0700 |
commit | f8d27ae2097092488116b8903418adf12fcee40e (patch) | |
tree | 2f3b1aed0d54a112eccdd22f57eca3b86ccba05b | |
parent | 556158f82180d72a7f9585d21a62320de3cd5dc0 (diff) |
initial commit
-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> |