Implementing CoreMotion

The CoreMotion framework implements many motion-related classes. The functionality for these classes varies, from counting steps with CMStepCounter, to figuring out the user's altitude with CMAltimeter, to accessing gyroscope data with CMGyroData, or even to reading whether a user is walking, running, or driving with CMMotionActivityManager. Reading the hardware sensors, such as the gyroscope, is done through an instance of CMMotionManager. The motion manager class provides an interface that enables developers to read data from the accelerometer, gyroscope, and more.

Our application will use a combination of sensors to function properly. However, before we get to that, we'll explore the available sensors a bit more because there are a lot of interesting features present in CoreMotion. We'll cover the pedometer first, and then we'll take a look at reading other sensors, such as altitude, accelerometer, and gyroscope.

If you use CoreMotion in your application, the new privacy restrictions in iOS 10 dictate that you must include the NSMotionUsageDescription key in your app's Info.plist. This key is similar to the one we used for the camera, except it should describe why we want to access motion data.