How to access the Step Counter and Pedometer Data in iOS 9

Since iOS 8 we can access data from the motion co-processors in iOS on compatible devices. One of those goodies is a pedometer (or step counter). With this new Core Motion class we can check how far we’ve travelled, how many steps we’ve taken, and even – since iOS 9 – how many flights of stairs we’ve walked up or down.

We can track live data as well as check the last seven days worth of data, both of which is only reliably available on devices with a motion co-processor.

Let’s see how many steps we’ve taken today!

Prep Work

We’ll be looking at two classes of the Core Motion framework: CMPedometer (the actual pedometer class), and CMPedometerData (properties returned from the pedometer).

To start with, we need to hold a reference to that pedometer so we can start and stop live tracking, or readout past data. I’ll do this with a custom initialiser and a property called pedometer. We also need to import the Core Motion framework so Xcode knows what we’re walking about:

Live Step Counting

To start receiving live updates from the pedometer, we’ll call the startPedometerUpdatesFromDate method. We need to provide an NSDate (such as “right now”). Part of the method signature is a handler block, in which any live updates are returned when available. This block is called for every new update:

Don’t be scared by such a long method: it simply checks if a property is available, and when it is update the label accordingly. It’s so long because we have 6 properties to check. Most values deliver a really long floating point number, so I’m using a number formatter to curb this to two digits (much easier on the eye).

If we no longer want to receive updates, we can call the stopPedometerUpdates on our pedometer:

One thing to note here is that when our app moves to the background, or the user turns off the device screen, these live updates will stop temporarily. It’s not like the CLLocationManager that delivers updates even when the app is not in the foreground.

Hence, live tracking steps isn’t that big a pleasure after all. Besides, it’s cumbersome having to switch it on before we want to count steps.

Lucky for us, Apple have implemented something even cooler:

Accessing past data

One great thing of the CMPedometer class is that no matter if we’ve started it for live updates, it keeps any steps from the last seven days in a cache we can access much faster. That’s how the Health App does it on iOS, and the plethora of step counting apps on the App Store.

All we need is a start date and an end date, with which we can call a very similar pedometer method named queryPedometerFromDate:toDate. Again we’ll get a handler block with all the properties I’ve mentioned above, albeit without the cadence and pace properties (because they only make sense for live updates):

This handler block is only called once. Note that if you want to grab data for an entire 24 hour period, you may need to normalise your NSDates (i.e. start on November 1st at 00:00:00, and end on November 1st at 23:59:59).