PyObjC HealthKit Framework
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.
Warnings
- breaking 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`).
- breaking 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.
- gotcha 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.
- gotcha 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.
- gotcha 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.
Install
-
pip install pyobjc-framework-healthkit
Imports
- HealthKit
import HealthKit
- HKHealthStore
from HealthKit import HKHealthStore
- HKObjectType
from HealthKit import HKObjectType
Quickstart
import HealthKit
from PyObjCTools import AppHelper
def request_health_authorization():
if not HealthKit.HKHealthStore.isHealthDataAvailable():
print("HealthKit is not available on this device.")
return
health_store = HealthKit.HKHealthStore.alloc().init()
# Define types to read and write
# Example: Body Mass and Height
try:
body_mass_type = HealthKit.HKObjectType.quantityTypeForIdentifier_(
HealthKit.HKQuantityTypeIdentifierBodyMass
)
height_type = HealthKit.HKObjectType.quantityTypeForIdentifier_(
HealthKit.HKQuantityTypeIdentifierHeight
)
except Exception as e:
print(f"Error getting HealthKit object types: {e}")
return
types_to_read = {body_mass_type, height_type}
types_to_share = set() # No data to write in this example
# Request authorization
# Note: This will trigger a system prompt to the user
def completion_handler(success, error):
if success:
print("HealthKit authorization granted.")
# You can now proceed with HealthKit operations
else:
print(f"HealthKit authorization denied or error: {error}")
AppHelper.stopEventLoop()
health_store.requestAuthorizationToShareTypes_readTypes_completion_(
types_to_share,
types_to_read,
completion_handler
)
if __name__ == '__main__':
# Run this from a macOS app context or a script with UI entitlements
# For a simple script, ensure PyObjCTools.AppHelper.runEventLoop() is used
# to handle asynchronous callbacks.
print("Requesting HealthKit authorization...")
request_health_authorization()
# For simple scripts where a UI application loop isn't explicitly started,
# AppHelper.runEventLoop() ensures callbacks are processed.
# In a full Cocoa app, the app's runloop would handle this.
AppHelper.runEventLoop()