Core Motion Activity Tracking in iOS 7

Written by: on September 18

One of the last minute surprises that Apple threw into the September 10th iPhone event was the existence of the M7 coprocessor in the new iPhone 5S. This new coprocessor exists solely to process data from the accelerometer, gyroscope, and compass. The M7 will allow a whole new breed of fitness, navigation or any motion based app, to get motion based data without running down your battery.

In order to make use of this new data in your apps, you will need to use the Core Motion Framework. Now Core Motion has been around since iOS 4.0, but iOS 7 adds the new classes needed to access data from the M7.

The two main classes you will be using are CMMotionActivityManager and CMMotionActivity. These two classes work hand in hand to bring you motion activity updates. You will use the CMMotionActivityManager class to start and stop the activity updates. The updates will be delivered to you in a series of CMMotionActivity objects. Each CMMotionActivity contains all the data for a single motion event. That data includes properties for…

@property(readonly, nonatomic) BOOL stationary;
@property(readonly, nonatomic) BOOL walking;
@property(readonly, nonatomic) BOOL running;
@property(readonly, nonatomic) BOOL automotive;

Each CMMotionActivity comes with a confidence and a startDate to help you better catalog the data.

The other new class available in the Core Motion Framework is CMStepCounter. You can determine if the device has the appropriate hardware for step counting by using the `+ isStepCountingAvailable’ method. Once you know that the device you are working with has the new M7 coprocessor you will start listening for steps with this method.

- (void)startStepCountingUpdatesToQueue:(NSOperationQueue *)queue
                           updateOn:(NSInteger)stepCounts
                        withHandler:(CMStepUpdateHandler)handler;

The interesting bit here is that you pass in the number of steps that you would like to be notified about. For instance passing in 20 for updateOn:(NSInteger)stepCounts would make sure that your handler would get called after every 20 steps.

The iPhone 5S will be always counting the steps — whether or not your app is listening for them. That means that even though a user might have just installed your app for the first time, you can query the system to get all the historical step data from that device. That query method is…

- (void)queryStepCountStartingFrom:(NSDate *)start to:(NSDate *)end toQueue:(NSOperationQueue *)queue withHandler:(CMStepQueryHandler)handler;

You can even pass in a startDate and endDate to query just the step data from a certain date range.

The new M7 coprocessor in the iPhone 5S is a really nice addition to the hardware. We can’t wait to see what all of you are going to do with the new API that it provides.

This is the eighth part in an 11-part Developer’s Guide to iOS 7. You can find the full guide here. For more information on how Double Encore can help you prepare your company for the changes in iOS 7, please email us.

Jay Graves

Jay is the Chief Technology Officer for Double Encore, Inc., a leading mobile development company. Jay’s expertise developing apps for some of the world's top brands has made him a respected leader in the space, with his work being featured on television, in iTunes and on devices inside Apple retail stores.

Article


Add your voice to the discussion: