{"id":25877275,"url":"https://github.com/siamakrostami/srhealthkitmanager","last_synced_at":"2026-02-13T08:10:50.251Z","repository":{"id":278916759,"uuid":"937167375","full_name":"siamakrostami/SRHealthKitManager","owner":"siamakrostami","description":"SRHealthKitManager is a lightweight and easy-to-use Swift framework for integrating Apple's HealthKit API into iOS and macOS applications. It simplifies authorization, data reading/writing, and data management while offering both async/await and callback-based APIs for flexibility.","archived":false,"fork":false,"pushed_at":"2025-02-22T14:24:38.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-22T14:37:53.151Z","etag":null,"topics":["async-await","healthkit","swift","threadsafe"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/siamakrostami.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-22T13:54:42.000Z","updated_at":"2025-02-22T14:24:06.000Z","dependencies_parsed_at":"2025-02-22T14:37:56.601Z","dependency_job_id":"cbd3842e-6638-415d-9f36-5cce3ad55a1c","html_url":"https://github.com/siamakrostami/SRHealthKitManager","commit_stats":null,"previous_names":["siamakrostami/srhealthkitmanager"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siamakrostami%2FSRHealthKitManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siamakrostami%2FSRHealthKitManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siamakrostami%2FSRHealthKitManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siamakrostami%2FSRHealthKitManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/siamakrostami","download_url":"https://codeload.github.com/siamakrostami/SRHealthKitManager/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241494860,"owners_count":19971995,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async-await","healthkit","swift","threadsafe"],"created_at":"2025-03-02T11:19:56.582Z","updated_at":"2026-02-13T08:10:50.223Z","avatar_url":"https://github.com/siamakrostami.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🚀 SRHealthKitManager\n\nA modern Swift framework that streamlines **HealthKit** integration in iOS and macOS applications.\n\n[![Swift](https://img.shields.io/badge/Swift-5.0-orange.svg)](https://swift.org)  \n[![Platform](https://img.shields.io/badge/platform-iOS%2013.0%2B%20%7C%20macOS%2013.0%2B-blue.svg)](https://developer.apple.com/ios/)  \n[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yourusername/SRHealthKitManager/blob/main/LICENSE)\n\n---\n\n## ✨ Overview\n\n**SRHealthKitManager** is a lightweight, intuitive Swift wrapper around Apple's **HealthKit** framework. It simplifies complex HealthKit operations using **modern Swift APIs**, including `async/await` and completion handlers.\n\n### 🔑 Key Features:\n✅ **Effortless HealthKit Authorization** – Request permissions with a single function  \n✅ **Seamless Data Management** – Read, write, and update HealthKit data effortlessly  \n✅ **Duplicate Detection \u0026 Filtering** – Avoid redundant data entries  \n✅ **Thread-Safe Operations** – Ensures data integrity and smooth performance  \n✅ **Flexible API** – Supports both `async/await` and callback-based execution  \n\n---\n\n## ⚙️ Requirements\n- 📱 iOS 13.0+ | 🖥️ macOS 13.0+\n- 🛠️ Swift 5.0+\n- 🏗️ Xcode 13.0+\n\n---\n\n## 📦 Installation\n\n### 🚀 Swift Package Manager (SPM)\nAdd SRHealthKitManager to your `Package.swift`:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/yourusername/SRHealthKitManager.git\", from: \"1.0.0\")\n]\n```\n\nOr install via Xcode:\n1. **File** → **Swift Packages** → **Add Package Dependency**\n2. Enter: `https://github.com/yourusername/SRHealthKitManager.git`\n3. Complete installation 🎉\n\n---\n\n## 🛠️ Setup \u0026 Configuration\n\n### 📌 Enable HealthKit in Xcode\n1. Open your project settings in Xcode  \n2. Go to **Signing \u0026 Capabilities**  \n3. Click **+ Capability** → Select **HealthKit**  \n\n### 🔐 Add Privacy Descriptions in `Info.plist`\n```xml\n\u003ckey\u003eNSHealthShareUsageDescription\u003c/key\u003e\n\u003cstring\u003eThis app needs access to your health data to provide personalized insights.\u003c/string\u003e\n\n\u003ckey\u003eNSHealthUpdateUsageDescription\u003c/key\u003e\n\u003cstring\u003eThis app needs to save health data to track your progress.\u003c/string\u003e\n```\n\n---\n\n## 🚀 Usage\n\n### 🏥 Requesting Authorization\n\n#### ✅ Using Callbacks\n```swift\nimport SRHealthKitManager\n\nHealthKitManager.shared.requestAuthorization { result in\n    switch result {\n    case .success(let success):\n        print(\"Authorization success: \\(success)\")\n    case .failure(let error):\n        print(\"Authorization failed: \\(error.localizedDescription)\")\n    }\n}\n```\n\n#### ✅ Using `async/await`\n```swift\nTask {\n    do {\n        let success = try await HealthKitManager.shared.requestAuthorization()\n        print(\"Authorization success: \\(success)\")\n    } catch {\n        print(\"Authorization failed: \\(error.localizedDescription)\")\n    }\n}\n```\n\n#### ✅ Requesting Authorization for Specific Types\n```swift\nlet typesToRead: Set\u003cHKObjectType\u003e = [\n    HealthKitTypes.heartRate,\n    HealthKitTypes.stepCount\n].compactMap { $0 }\n\nlet typesToWrite: Set\u003cHKSampleType\u003e = [\n    HealthKitTypes.bodyMass\n].compactMap { $0 }\n\nTask {\n    do {\n        let success = try await HealthKitManager.shared.requestAuthorization(\n            toShare: typesToWrite, \n            read: typesToRead\n        )\n        print(\"Specific authorization success: \\(success)\")\n    } catch {\n        print(\"Specific authorization failed: \\(error.localizedDescription)\")\n    }\n}\n```\n\n---\n\n### 📊 Reading Data\n\n#### ✅ Using Callbacks\n```swift\nHealthKitManager.shared.readSamples(\n    ofType: HealthKitTypes.stepCount!,\n    predicate: HKQuery.predicateForSamples(\n        withStart: Date().addingTimeInterval(-86400),\n        end: Date(),\n        options: .strictStartDate\n    )\n) { result in\n    switch result {\n    case .success(let samples):\n        for sample in samples {\n            if let quantitySample = sample as? HKQuantitySample {\n                let stepCount = quantitySample.quantity.doubleValue(for: HKUnit.count())\n                print(\"Steps: \\(stepCount)\")\n            }\n        }\n    case .failure(let error):\n        print(\"Failed to read step count: \\(error.localizedDescription)\")\n    }\n}\n```\n\n#### ✅ Using `async/await`\n```swift\nTask {\n    do {\n        let samples = try await HealthKitManager.shared.readSamples(\n            ofType: HealthKitTypes.heartRate!,\n            predicate: HKQuery.predicateForSamples(\n                withStart: Date().addingTimeInterval(-86400),\n                end: Date(),\n                options: .strictStartDate\n            ),\n            sortDescriptors: [NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)]\n        )\n        \n        for sample in samples {\n            if let quantitySample = sample as? HKQuantitySample {\n                let heartRate = quantitySample.quantity.doubleValue(for: HKUnit.count().unitDivided(by: HKUnit.minute()))\n                print(\"Heart rate: \\(heartRate) BPM\")\n            }\n        }\n    } catch {\n        print(\"Failed to read heart rate: \\(error.localizedDescription)\")\n    }\n}\n```\n\n---\n\n### ✍️ Writing Data\n\n#### ✅ Writing Single Sample\n```swift\nlet stepCountSample = HKQuantitySample(\n    type: HealthKitTypes.stepCount!,\n    quantity: HKQuantity(unit: HKUnit.count(), doubleValue: 1000),\n    start: Date().addingTimeInterval(-3600),\n    end: Date()\n)\n\n// Using async/await\nTask {\n    do {\n        let success = try await HealthKitManager.shared.save(stepCountSample)\n        print(\"Step count saved successfully: \\(success)\")\n    } catch {\n        print(\"Failed to save step count: \\(error.localizedDescription)\")\n    }\n}\n```\n\n#### ✅ Saving Multiple Samples\n```swift\nlet samples = [stepCountSample, anotherSample, yetAnotherSample]\n\nTask {\n    do {\n        let success = try await HealthKitManager.shared.saveBatch(samples)\n        print(\"Samples saved successfully: \\(success)\")\n    } catch {\n        print(\"Failed to save samples: \\(error.localizedDescription)\")\n    }\n}\n```\n\n---\n\n### 🔍 Checking Authorization Status\n\n```swift\nlet typesToCheck: Set\u003cHKSampleType\u003e = [\n    HealthKitTypes.heartRate,\n    HealthKitTypes.stepCount\n].compactMap { $0 }\n\nTask {\n    let isAuthorized = try await HealthKitManager.shared.checkAuthorizationStatus(for: typesToCheck)\n    print(\"Is authorized (async): \\(isAuthorized)\")\n}\n```\n\n---\n\n## ❌ Error Handling\n\nSRHealthKitManager defines clear, **Swift-native** error types:\n\n```swift\npublic enum HealthKitError: LocalizedError, Sendable {\n    case healthKitNotAvailable\n    case unauthorizedForType(String)\n    case saveFailed(String)\n    case invalidData\n    case dataNotAvailable\n}\n```\n\n---\n\n## 🏃‍♂️ Advanced Usage\n\n### 🏋️‍♂️ Working with Workouts\n```swift\nlet workoutType = HKObjectType.workoutType()\nlet predicate = HKQuery.predicateForWorkouts(with: .running)\n\nTask {\n    do {\n        let workouts = try await HealthKitManager.shared.readSamples(\n            ofType: workoutType,\n            predicate: predicate\n        ) as? [HKWorkout]\n\n        for workout in workouts ?? [] {\n            print(\"Workout: \\(workout.workoutActivityType.name)\")\n            print(\"Duration: \\(workout.duration) seconds\")\n        }\n    } catch {\n        print(\"Error fetching workouts: \\(error.localizedDescription)\")\n    }\n}\n```\n\n---\n\n## 📱 Example Project\n\nLooking to get started quickly? Check out our example project in the [Example/SRHealthKitManagerExample](Example/SRHealthKitManagerExample) directory. This SwiftUI-based example app demonstrates:\n\n🔍 Complete implementation of:\n- HealthKit authorization flow\n- Reading step count and heart rate data\n- Writing workout data\n- Error handling and user feedback\n- SwiftUI integration with MVVM architecture\n\n### 🚀 Running the Example\n\n1. Clone this repository\n2. Open `Example/SRHealthKitManagerExample/SRHealthKitManagerExample.xcodeproj`\n3. Select your development team\n4. Run on a physical iOS device (HealthKit is not available in simulators)\n\n\u003e **Note**: Make sure to run the example on a physical iOS device, as HealthKit functionality is not available in the iOS Simulator.\n\n---\n\n## 📜 License\n\nSRHealthKitManager is available under the **MIT License**. See the `LICENSE` file for details.\n\n---\n\n## 🙌 Contributing\n\nContributions are welcome! Feel free to **submit a pull request** or **open an issue**. 🚀\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiamakrostami%2Fsrhealthkitmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsiamakrostami%2Fsrhealthkitmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiamakrostami%2Fsrhealthkitmanager/lists"}