Programmatically accessing your iOS health data (2022) 💾

healthsoftwareiOS

As it turns out, programmatically accessing your own health data can still be a headache in 2022.

I recently wanted to integrate access for an automation on my website around my daily movement, and I found it to take longer than expected.

Ultimately, I found three ways of accessing this data:

Shortcuts

The built-in Shortcuts iOS app is a great way to automate tasks on your iPhone, and accessing heath data happens to work here too.

You can start by creating the shortcut either in the Shortcuts tab or Automation tab of the app, depending on whether you want this to run automatically on a schedule or not.

As far as the shortcut itself goes, you can use the Find All Health Samples Where action to retrieve the data you want.

As you can see above, once you've selected the Find All Health Samples action, you can filter for the start/end dates of the data, the source of the data, the units of the data, and the data type itself.

Afterwards, you can use the Calculate Statistics action to calculate the Sum, for example, of the data you've retrieved for the date range.

Finally you can export the data, eg. email the JSON to yourself, integrate with a shell script over SSH, etc.

As a reminder, you can have this Shortcut run as an automation or on-demand depending on your use-case.

Native iOS app

If you want the most flexibility and can code, this is a good option.

You'll want to glance at the Apple HealthKit API (opens in a new tab) to get a sense of what you'll be using to read and transform your data.

I used Apple's Mobility Health App (opens in a new tab) sample project as a starting point before integrating my database and accessing the specific data points I wanted.

In my case, I wanted to read the following data points:

I also wanted to be able to aggregate data points over a custom time period.

And because this is your own app, you can set it to run at any frequency you want, upload the data to your own database or as a JSON file, seamlessly integrate with your own APIs etc.

Export from iOS Health app

This option is the least practical/flexible depending on your use-case. If you want all the data once on a non-recurring basis, then this could work well.

Simply put, you can access this feature in the Health app by navigating to you profile, then scrolling down to the bottom of the screen, past the Features and Privacy sections, and tapping on Export All Health Data.

This'll take awhile depending on how much data you have, but once it's done, you'll have an XML file with all of your health data.

See the official Apple documentation (opens in a new tab) for more details.


Last Updated: Sun Nov 27 2022

Related Posts

The methodology around my accountability trackers 👨‍🔬

Wed Nov 23 2022

Looking for more posts?


© Gabriel Uribe.RSS