Class

CMMotionManager

The object for starting and managing motion services.

Declaration

@interface CMMotionManager : NSObject

Overview

Use a CMMotionManager object to start the services that report movement detected by the device's onboard sensors. Use this object to receive four types of motion data:

  • Accelerometer data, indicating the instantaneous acceleration of the device in three dimensional space.

  • Gyroscope data, indicating the instantaneous rotation around the device's three primary axes.

  • Magnetometer data, indicating the device's orientation relative to Earth's magnetic field.

  • Device-motion data, indicating key motion-related attributes such as the device's user-initiated acceleration, its attitude, rotation rates, orientation relative to calibrated magnetic fields, and orientation relative to gravity. This data is provided by Core Motion’s sensor fusion algorithms.

The processed device-motion data gives the device’s attitude, rotation rate, calibrated magnetic fields, the direction of gravity, and the acceleration the user is imparting to the device.

You can receive live sensor data at a specified update intervals, or you can let the sensors collect data and store it for retrieval later. With both of these approaches, call the appropriate stop method (stopAccelerometerUpdates, stopGyroUpdates, stopMagnetometerUpdates, and stopDeviceMotionUpdates) when you no longer need the data.

Handling Motion Updates at Specified Intervals

To receive motion data at specific intervals, the app calls a “start” method that takes an operation queue (instance of NSOperationQueue) and a block handler of a specific type for processing those updates. The motion data is passed into the block handler. The frequency of updates is determined by the value of an “interval” property.

Periodic Sampling of Motion Data

To handle motion data by periodic sampling, the app calls a “start” method taking no arguments and periodically accesses the motion data held by a property for a given type of motion data. This approach is the recommended approach for apps such as games. Handling accelerometer data in a block introduces additional overhead, and most game apps are interested only the latest sample of motion data when they render a frame.

Hardware Availability and State

If a hardware feature (for example, a gyroscope) is not available on a device, calling a start method related to that feature has no effect. You can find out whether a hardware feature is available or active by checking the appropriate property; for example, for gyroscope data, you can check the value of the gyroAvailable or gyroActive properties.

Topics

Determining the Availability of Services

deviceMotionAvailable

A Boolean value that indicates whether the device-motion service is available on the device.

accelerometerAvailable

A Boolean value that indicates whether an accelerometer is available on the device.

gyroAvailable

A Boolean value that indicates whether a gyroscope is available on the device.

magnetometerAvailable

A Boolean value that indicates whether a magnetometer is available on the device.

Determining Which Services Are Active

deviceMotionActive

A Boolean value that determines whether the app is receiving updates from the device-motion service.

accelerometerActive

A Boolean value that indicates whether accelerometer updates are currently happening.

gyroActive

A Boolean value that determines whether gyroscope updates are currently happening.

magnetometerActive

A Boolean value that determines whether magnetometer updates are currently happening.

Managing Device Motion Updates

showsDeviceMovementDisplay

Controls whether the device-movement display is shown.

deviceMotionUpdateInterval

The interval, in seconds, for providing device-motion updates to the block handler.

- startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler:

Starts device-motion updates on an operation queue and using a specified reference frame and block handler.

- startDeviceMotionUpdatesToQueue:withHandler:

Starts device-motion updates on an operation queue and using a specified block handler.

- startDeviceMotionUpdatesUsingReferenceFrame:

Starts device-motion updates using a reference frame but without a block handler.

- startDeviceMotionUpdates

Starts device-motion updates without a block handler.

- stopDeviceMotionUpdates

Stops device-motion updates.

deviceMotion

The latest sample of device-motion data.

CMDeviceMotionHandler

The type of block callback for handling device-motion data.

Managing Accelerometer Updates

accelerometerUpdateInterval

The interval, in seconds, for providing accelerometer updates to the block handler.

- startAccelerometerUpdatesToQueue:withHandler:

Starts accelerometer updates on an operation queue and with a specified handler.

- startAccelerometerUpdates

Starts accelerometer updates without a handler.

- stopAccelerometerUpdates

Stops accelerometer updates.

accelerometerData

The latest sample of accelerometer data.

CMAccelerometerHandler

The type of block callback for handling accelerometer data.

Managing Gyroscope Updates

gyroUpdateInterval

The interval, in seconds, for providing gyroscope updates to the block handler.

- startGyroUpdatesToQueue:withHandler:

Starts gyroscope updates on an operation queue and with a specified handler.

- startGyroUpdates

Starts gyroscope updates without a handler.

- stopGyroUpdates

Stops gyroscope updates.

gyroData

The latest sample of gyroscope data.

CMGyroHandler

The type of block callback for handling gyroscope data.

Managing Magnetometer Updates

magnetometerUpdateInterval

The interval, in seconds, at which the system delivers magnetometer data to the block handler.

- startMagnetometerUpdatesToQueue:withHandler:

Starts magnetometer updates on an operation queue and with a specified handler.

- startMagnetometerUpdates

Starts magnetometer updates without a block handler.

- stopMagnetometerUpdates

Stops magnetometer updates.

magnetometerData

The latest sample of magnetometer data.

CMMagnetometerHandler

The type of block callback for handling magnetometer data.

Accessing Attitude Reference Frames

attitudeReferenceFrame

Returns either the reference frame currently being used or the default attitude reference frame.

+ availableAttitudeReferenceFrames

Returns a bitmask specifying the available attitude reference frames on the device.

CMAttitudeReferenceFrame

Enum constants for indicating the reference frames from which all attitude samples are referenced.

Understanding Errors

CMErrorDomain

The error domain for Core Motion.

CMError

Defines a data type for a ColorSync Manager result code.

Relationships

Inherits From