{"id":6113,"library":"pyobjc-framework-healthkit","title":"PyObjC HealthKit Framework","description":"PyObjC-framework-HealthKit provides Python bindings for Apple's HealthKit framework on macOS. It allows Python applications to interact with health and fitness data, offering a bridge to Objective-C APIs. The library is actively maintained with frequent releases, often synchronized with macOS SDK updates and changes in Python version support.","status":"active","version":"12.1","language":"en","source_language":"en","source_url":"https://github.com/ronaldoussoren/pyobjc","tags":["macOS","Apple","HealthKit","Objective-C bridge","PyObjC","automation"],"install":[{"cmd":"pip install pyobjc-framework-healthkit","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core bridge between Python and Objective-C, required for all PyObjC framework wrappers.","package":"pyobjc-core"}],"imports":[{"symbol":"HealthKit","correct":"import HealthKit"},{"symbol":"HKHealthStore","correct":"from HealthKit import HKHealthStore"},{"symbol":"HKObjectType","correct":"from HealthKit import HKObjectType"}],"quickstart":{"code":"import HealthKit\nfrom PyObjCTools import AppHelper\n\ndef request_health_authorization():\n    if not HealthKit.HKHealthStore.isHealthDataAvailable():\n        print(\"HealthKit is not available on this device.\")\n        return\n\n    health_store = HealthKit.HKHealthStore.alloc().init()\n\n    # Define types to read and write\n    # Example: Body Mass and Height\n    try:\n        body_mass_type = HealthKit.HKObjectType.quantityTypeForIdentifier_(\n            HealthKit.HKQuantityTypeIdentifierBodyMass\n        )\n        height_type = HealthKit.HKObjectType.quantityTypeForIdentifier_(\n            HealthKit.HKQuantityTypeIdentifierHeight\n        )\n    except Exception as e:\n        print(f\"Error getting HealthKit object types: {e}\")\n        return\n\n    types_to_read = {body_mass_type, height_type}\n    types_to_share = set() # No data to write in this example\n\n    # Request authorization\n    # Note: This will trigger a system prompt to the user\n    def completion_handler(success, error):\n        if success:\n            print(\"HealthKit authorization granted.\")\n            # You can now proceed with HealthKit operations\n        else:\n            print(f\"HealthKit authorization denied or error: {error}\")\n        AppHelper.stopEventLoop()\n\n    health_store.requestAuthorizationToShareTypes_readTypes_completion_(\n        types_to_share,\n        types_to_read,\n        completion_handler\n    )\n\n\nif __name__ == '__main__':\n    # Run this from a macOS app context or a script with UI entitlements\n    # For a simple script, ensure PyObjCTools.AppHelper.runEventLoop() is used\n    # to handle asynchronous callbacks.\n    print(\"Requesting HealthKit authorization...\")\n    request_health_authorization()\n    # For simple scripts where a UI application loop isn't explicitly started,\n    # AppHelper.runEventLoop() ensures callbacks are processed.\n    # In a full Cocoa app, the app's runloop would handle this.\n    AppHelper.runEventLoop()\n","lang":"python","description":"This quickstart demonstrates how to check for HealthKit availability and request authorization to read specific data types (Body Mass and Height). Running this code will trigger a system prompt asking the user for permission. HealthKit operations require an active run loop to handle asynchronous callbacks. For a standalone script, `PyObjCTools.AppHelper.runEventLoop()` is essential to process these callbacks and wait for the authorization request to complete. Note that HealthKit features require an application with the proper entitlements set in Xcode."},"warnings":[{"fix":"Upgrade Python to a supported version (e.g., 3.10+ for PyObjC 12.x).","message":"PyObjC drops support for older Python versions with major releases. Python 3.9 support was dropped in PyObjC v12.0, and Python 3.8 support was dropped in v11.0. Ensure your Python environment meets the `requires_python` spec (currently `>=3.10`).","severity":"breaking","affected_versions":">=11.0"},{"fix":"Review custom `init` method implementations and their Python wrappers, especially regarding reference counting, to ensure compatibility with ARC semantics.","message":"PyObjC v11.1 aligned its behavior for initializer methods with `clang`'s Automatic Reference Counting (ARC) documentation. Methods in the 'init' family now correctly steal a reference to `self` and return a new reference. This change can impact custom Objective-C `init` method wrappers if not handled correctly.","severity":"breaking","affected_versions":">=11.1"},{"fix":"Configure your macOS application's Xcode project to include the HealthKit capability and add the necessary privacy usage descriptions to its `Info.plist` file.","message":"HealthKit functionality requires specific entitlements (`HealthKit` capability) and privacy descriptions (`Privacy - Health Share Usage Description`, `Privacy - Health Update Usage Description`) added to your application's `Info.plist` file within Xcode. Without these, authorization requests will fail or the framework will be unavailable.","severity":"gotcha","affected_versions":"all"},{"fix":"Avoid using Python's experimental free-threading (`python -X 'gil=0'`) when developing applications that rely on PyObjC frameworks. Use standard Python threading if needed.","message":"PyObjC does not support the experimental free-threading (PEP 703) feature introduced in Python 3.13. While binary wheels might be available for Python 3.13, using free-threading with PyObjC can lead to instability or incorrect behavior.","severity":"gotcha","affected_versions":">=10.3"},{"fix":"Wrap all HealthKit-dependent code paths with a check for `HealthKit.HKHealthStore.isHealthDataAvailable()` to gracefully handle environments where HealthKit is not present.","message":"HealthKit is not universally available on all macOS devices or operating system versions (e.g., it might not be available on older macOS versions or specific hardware configurations). Always check `HKHealthStore.isHealthDataAvailable()` before attempting any HealthKit operations.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}