CMMotionManager Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/CoreMotion.framework |
| Availability | Available in iOS 4.0 and later. |
| Companion guide | |
| Declared in | CMError.h CMErrorDomain.h CMMotionManager.h |
Overview
A CMMotionManager object is the gateway to the motion services provided by iOS. These services provide an app with accelerometer data, rotation-rate data, magnetometer data, and other device-motion data such as attitude. These types of data originate with a device’s accelerometers and (on some models) its magnetometer and gyroscope.
After creating an instance of CMMotionManager, an app can use it to receive four types of motion: raw accelerometer data, raw gyroscope data, raw magnetometer data, and processed device-motion data (which includes accelerometer, rotation-rate, and attitude measurements). The processed device-motion data provided by Core Motion’s sensor fusion algorithms gives the device’s attitude, rotation rate, calibrated magnetic fields, the direction of gravity, and the acceleration the user is imparting to the device.
An app can take one of two approaches when receiving motion data, by handling it at specified update intervals or periodically sampling the motion data. With both of these approaches, the app should call the appropriate stop method (stopAccelerometerUpdates, stopGyroUpdates, stopMagnetometerUpdates, and stopDeviceMotionUpdates) when it has finished processing accelerometer, rotation-rate, magnetometer, or device-motion 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.
Accelerometer. Set the
accelerometerUpdateIntervalproperty to specify an update interval. Call thestartAccelerometerUpdatesToQueue:withHandler:method, passing in a block of typeCMAccelerometerHandler. Accelerometer data is passed into the block asCMAccelerometerDataobjects.Gyroscope. Set the
gyroUpdateIntervalproperty to specify an update interval. Call thestartGyroUpdatesToQueue:withHandler:method, passing in a block of typeCMGyroHandler. Rotation-rate data is passed into the block asCMGyroDataobjects.Magnetometer. Set the
magnetometerUpdateIntervalproperty to specify an update interval. Call thestartMagnetometerUpdatesToQueue:withHandler:method, passing a block of typeCMMagnetometerHandler. Magnetic-field data is passed into the block asCMMagnetometerDataobjects.Device motion. Set the
deviceMotionUpdateIntervalproperty to specify an update interval. Call thestartDeviceMotionUpdatesUsingReferenceFrame:orstartDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler:orstartDeviceMotionUpdatesToQueue:withHandler:method, passing in a block of typeCMDeviceMotionHandler. With the former method (new in iOS 5.0), you can specify a reference frame to be used for the attitude estimates. Rotation-rate data is passed into the block asCMDeviceMotionobjects.
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.
Accelerometer. Call
startAccelerometerUpdatesto begin updates and periodically accessCMAccelerometerDataobjects by reading theaccelerometerDataproperty.Gyroscope. Call
startGyroUpdatesto begin updates and periodically accessCMGyroDataobjects by reading thegyroDataproperty.Magnetometer. Call
startMagnetometerUpdatesto begin updates and periodically accessCMMagnetometerDataobjects by reading themagnetometerDataproperty.Device motion. Call the
startDeviceMotionUpdatesUsingReferenceFrame:orstartDeviceMotionUpdatesmethod to begin updates and periodically accessCMDeviceMotionobjects by reading thedeviceMotionproperty. ThestartDeviceMotionUpdatesUsingReferenceFrame:method (new in iOS 5.0) lets you specify a reference frame to be used for the attitude estimates.
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.
Tasks
Managing Accelerometer Updates
-
accelerometerUpdateIntervalproperty -
– startAccelerometerUpdatesToQueue:withHandler: -
– startAccelerometerUpdates -
– stopAccelerometerUpdates
Determining Whether the Accelerometer Is Active and Available
-
accelerometerActiveproperty -
accelerometerAvailableproperty
Accessing Accelerometer Data
-
accelerometerDataproperty
Managing Gyroscope Updates
-
gyroUpdateIntervalproperty -
– startGyroUpdatesToQueue:withHandler: -
– startGyroUpdates -
– stopGyroUpdates
Determining Whether the Gyroscope Is Active and Available
-
gyroActiveproperty -
gyroAvailableproperty
Accessing Gyroscope Data
-
gyroDataproperty
Managing Magnetometer Updates
-
magnetometerUpdateIntervalproperty -
– startMagnetometerUpdatesToQueue:withHandler: -
– startMagnetometerUpdates -
– stopMagnetometerUpdates
Determining Whether the Magnetometer Is Active and Available
-
magnetometerActiveproperty -
magnetometerAvailableproperty
Accessing Magnetometer Data
-
magnetometerDataproperty
Managing the Device Movement Display
-
showsDeviceMovementDisplayproperty
Managing Device Motion Updates
-
deviceMotionUpdateIntervalproperty -
– startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler: -
– startDeviceMotionUpdatesToQueue:withHandler: -
– startDeviceMotionUpdatesUsingReferenceFrame: -
– startDeviceMotionUpdates -
– stopDeviceMotionUpdates
Accessing Attitude Reference Frames
Determining Whether the Device Motion Hardware Is Active and Available
-
deviceMotionActiveproperty -
deviceMotionAvailableproperty
Accessing Device Motion Data
-
deviceMotionproperty
Properties
accelerometerActive
A Boolean value that indicates whether accelerometer updates are currently happening. (read-only)
Discussion
This property indicates whether startAccelerometerUpdatesToQueue:withHandler: or startAccelerometerUpdates has been called since the last time stopAccelerometerUpdates was called. (If the start methods hadn’t been called, the app could be getting updates from the accelerometer after calling, for example, startDeviceMotionUpdates, but this property would return NO.)
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
CMMotionManager.haccelerometerAvailable
A Boolean value that indicates whether an accelerometer is available on the device. (read-only)
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
CMMotionManager.haccelerometerData
The latest sample of accelerometer data. (read-only)
Discussion
If no accelerometer data is available, the value of this property is nil. An app that is receiving accelerometer data after calling startAccelerometerUpdates periodically checks the value of this property and processes the acceleration data.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.haccelerometerUpdateInterval
The interval, in seconds, for providing accelerometer updates to the block handler.
Discussion
The system supplies accelerometer updates to the block handler specified in startAccelerometerUpdatesToQueue:withHandler: at regular intervals determined by the value of this property. The interval units are in seconds. The value of this property is capped to minimum and maximum values; the maximum value is determined by the maximum frequency supported by the hardware. If your app is sensitive to the intervals of acceleration data, it should always check the timestamps of the delivered CMAccelerometerData instances to determine the true update interval.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hattitudeReferenceFrame
Returns either the reference frame currently being used or the default attitude reference frame (read-only)
Discussion
If device motion is active, this property returns the reference frame currently in use. If device motion is not active but has been active since the app was last launched, this property returns the last frame used. If device motion has not been active since the app was last launched, this property returns the default attitude reference frame for the device. If device motion is not available on the device, the value is undefined.
Availability
- Available in iOS 5.0 and later.
Declared In
CMMotionManager.hdeviceMotion
The latest sample of device-motion data. (read-only)
Discussion
If no device-motion data is available, the value of this property is nil. An app that is receiving device-motion data after calling startDeviceMotionUpdates periodically checks the value of this property and processes the device-motion data.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hdeviceMotionActive
A Boolean value that determines whether the app is receiving updates from the device-motion service. (read-only)
Discussion
This property indicates whether startDeviceMotionUpdatesToQueue:withHandler: or startDeviceMotionUpdates has been called since the last time stopDeviceMotionUpdates was called.
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
CMMotionManager.hdeviceMotionAvailable
A Boolean value that indicates whether the device-motion service is available on the device. (read-only)
Discussion
The device-motion service is available if a device has both an accelerometer and a gyroscope. Because all devices have accelerometers, this property is functionally equivalent to gyroAvailable.
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
CMMotionManager.hdeviceMotionUpdateInterval
The interval, in seconds, for providing device-motion updates to the block handler.
Discussion
The system supplies device-motion updates to the block handler specified in startDeviceMotionUpdatesToQueue:withHandler: at regular intervals determined by the value of this property. The interval units are in seconds. The value of this property is capped to minimum and maximum values; the maximum value is determined by the maximum frequency supported by the hardware. If your app is sensitive to the intervals of device-motion data, it should always check the timestamps of the delivered CMDeviceMotion instances to determine the true update interval.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hgyroActive
A Boolean value that determines whether gyroscope updates are currently happening. (read-only)
Discussion
This property indicates whether startGyroUpdatesToQueue:withHandler: or startGyroUpdates has been called since the last time stopGyroUpdates was called. (If the start methods hadn’t been called, the app could be getting updates from the gyroscope after calling, for example, startDeviceMotionUpdates, but this property would return NO.)
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
CMMotionManager.hgyroAvailable
A Boolean value that indicates whether a gyroscope is available on the device. (read-only)
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
CMMotionManager.hgyroData
The latest sample of gyroscope data. (read-only)
Discussion
If no gyroscope data is available, the value of this property is nil. An app that is receiving gyroscope data after calling startGyroUpdates periodically checks the value of this property and processes the gyroscope data.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hgyroUpdateInterval
The interval, in seconds, for providing gyroscope updates to the block handler.
Discussion
The system supplies gyroscope (that is, rotation rate) updates to the block handler specified in startGyroUpdatesToQueue:withHandler: at regular intervals determined by the value of this property. The interval units are in seconds. The value of this property is capped to minimum and maximum values; the maximum value is determined by the maximum frequency supported by the hardware. If your app is sensitive to the intervals of gyroscope data, it should always check the timestamps of the delivered CMGyroData instances to determine the true update interval.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hmagnetometerActive
A Boolean value that determines whether magnetometer updates are currently happening. (read-only)
Discussion
This property indicates whether the startMagnetometerUpdatesToQueue:withHandler: or startMagnetometerUpdates method has been called since the last time the stopMagnetometerUpdates method was called. (If the start methods hadn’t been called, the app could be getting updates from the magnetometer after calling, for example, startDeviceMotionUpdates, but this property would return NO.)
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
CMMotionManager.hmagnetometerAvailable
A Boolean value that indicates whether a magnetometer is available on the device. (read-only)
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
CMMotionManager.hmagnetometerData
The latest sample of magnetometer data. (read-only)
Discussion
If no magnetometer data is available, the value of this property is nil. An app that is receiving magnetometer data after calling startMagnetometerUpdates periodically checks the value of this property and processes the gyroscope data.
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
CMMotionManager.hmagnetometerUpdateInterval
The interval, in seconds, at which the system delivers magnetometer data to the block handler.
Discussion
The supplies magnetometer data to the block handler specified in startMagnetometerUpdatesToQueue:withHandler: at regular intervals determined by the value of this property. The interval unit are in seconds. The value of this property is capped to minimum and maximum values; the maximum value is determined by the maximum frequency supported by the hardware. If your app is sensitive to the intervals of magnetometer data, it should always check the timestamps of the delivered CMMagnetometerData instances to determine the true update interval.
Availability
- Available in iOS 5.0 and later.
Declared In
CMMotionManager.hshowsDeviceMovementDisplay
Controls whether the device-movement display is shown.
Discussion
When a device requires movement (for example, to calibrate the compass), the value of this property indicates if the system’s device-movement display should be shown. When a device requires movement, the block handler of type CMDeviceMotionHandler reports the CMErrorDeviceRequiresMovement error once. By default, this property is NO.
Availability
- Available in iOS 5.0 and later.
Declared In
CMMotionManager.hClass Methods
availableAttitudeReferenceFrames
Returns a bitmask specifying the available attitude reference frames on the device.
Return Value
A bitmask that you can bitwise-AND with the enum constants of the CMAttitudeReferenceFrame type.
Discussion
For example, to determine whether CMAttitudeReferenceFrameXMagneticNorthZVertical is available on the device, you would perform the following test:
if ([CMMotionManager availableAttitudeReferenceFrames] & CMAttitudeReferenceFrameXMagneticNorthZVertical) { |
// do something appropriate here |
} |
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
CMMotionManager.hInstance Methods
startAccelerometerUpdates
Starts accelerometer updates without a handler.
Discussion
You can get the latest accelerometer data through the accelerometerData property. You must call stopAccelerometerUpdates when you no longer want your app to process accelerometer updates.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hstartAccelerometerUpdatesToQueue:withHandler:
Starts accelerometer updates on an operation queue and with a specified handler.
Parameters
- queue
An operation queue provided by the caller. Because the processed events might arrive at a high rate, using the main operation queue is not recommended.
- handler
A block that is invoked with each update to handle new accelerometer data. The block must conform to the
CMAccelerometerHandlertype.
Discussion
You must call stopAccelerometerUpdates when you no longer want your app to process accelerometer updates.
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
CMMotionManager.hstartDeviceMotionUpdates
Starts device-motion updates without a block handler.
Discussion
You can get the latest device-motion data through the deviceMotion property. You must call stopDeviceMotionUpdates when you no longer want your app to process device-motion updates. This method uses the reference frame returned by attitudeReferenceFrame for device-motion updates.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hstartDeviceMotionUpdatesToQueue:withHandler:
Starts device-motion updates on an operation queue and using a specified block handler.
Parameters
- queue
An operation queue provided by the caller. Because the processed events might arrive at a high rate, using the main operation queue is not recommended.
- handler
A block that is invoked with each update to handle new device-motion data. The block must conform to the
CMDeviceMotionHandlertype.
Discussion
This method uses the reference frame returned by attitudeReferenceFrame for device-motion updates. You must call stopDeviceMotionUpdates when you no longer want your app to process device-motion updates.
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
CMMotionManager.hstartDeviceMotionUpdatesUsingReferenceFrame:
Starts device-motion updates using a reference frame but without a block handler.
Parameters
- referenceFrame
A constant identifying the reference frame to use for device-motion updates.
Discussion
You can get the latest device-motion data through the deviceMotion property. You must call stopDeviceMotionUpdates when you no longer want your app to process device-motion updates.
Availability
- Available in iOS 5.0 and later.
Declared In
CMMotionManager.hstartDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler:
Starts device-motion updates on an operation queue and using a specified reference frame and block handler.
Parameters
- referenceFrame
A constant identifying the reference frame to use for device-motion updates.
- queue
An operation queue provided by the caller. Because the processed events might arrive at a high rate, using the main operation queue is not recommended.
- handler
A block that is invoked with each update to handle new device-motion data. The block must conform to the
CMDeviceMotionHandlertype.
Discussion
You must call stopDeviceMotionUpdates when you no longer want your app to process device-motion updates.
Availability
- Available in iOS 5.0 and later.
Declared In
CMMotionManager.hstartGyroUpdates
Starts gyroscope updates without a handler.
Discussion
You can get the latest gyroscope data through the gyroData property. You must call stopGyroUpdates when you no longer want your app to process gyroscope updates.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hstartGyroUpdatesToQueue:withHandler:
Starts gyroscope updates on an operation queue and with a specified handler.
Parameters
- queue
An operation queue provided by the caller. Because the processed events might arrive at a high rate, using the main operation queue is not recommended.
- handler
A block that is invoked with each update to handle new gyroscope data. The block must conform to the
CMGyroHandlertype.
Discussion
You must call stopGyroUpdates when you no longer want your app to process gyroscope updates.
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
CMMotionManager.hstartMagnetometerUpdates
Starts magnetometer updates without a block handler.
Discussion
You can get the latest magnetometer data through the magnetometerData property. You must call stopMagnetometerUpdates when you no longer want your app to process magnetometer updates.
Availability
- Available in iOS 5.0 and later.
Declared In
CMMotionManager.hstartMagnetometerUpdatesToQueue:withHandler:
Starts magnetometer updates on an operation queue and with a specified handler.
Parameters
- queue
An operation queue provided by the caller. Because the processed events might arrive at a high rate, using the main operation queue is not recommended.
- handler
A block that is invoked with each update to handle new magnetometer data. The block must conform to the
CMMagnetometerHandlertype.
Discussion
You must call stopMagnetometerUpdates when you no longer want your app to process magnetometer updates.
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
CMMotionManager.hstopAccelerometerUpdates
Stops accelerometer updates.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hstopDeviceMotionUpdates
Stops device-motion updates.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hstopGyroUpdates
Stops gyroscope updates.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hConstants
CMAccelerometerHandler
The type of block callback for handling accelerometer data.
typedef void (^CMAccelerometerHandler)(CMAccelerometerData *accelerometerData, NSError *error);
Discussion
Blocks of type CMAccelerometerHandler are called when there is accelerometer data to process. You pass the block into startAccelerometerUpdatesToQueue:withHandler: as the second argument. Blocks of this type return no value but take two arguments:
- accelerometerData
An object that encapsulates a
CMAccelerationstructure with fields holding acceleration values for the three axes of movement.- error
An error object representing an error encountered in providing accelerometer updates. If an error occurs, you should stop accelerometer updates and inform the user of the problem. If there is no error, this argument is
nil. Core Motion errors are of theCMErrorDomaindomain and theCMErrortype.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hCMGyroHandler
The type of block callback for handling gyroscope data.
typedef void (^CMGyroHandler)(CMGyroData *gyroData, NSError *error);
Discussion
Blocks of type CMGyroHandler are called when there is gyroscope data to process. You pass the block into startGyroUpdatesToQueue:withHandler: as the second argument. Blocks of this type return no value but take two arguments:
- gyroData
An object that encapsulates a
CMRotationRatestructure with fields holding rotation-rate values for the three axes of movement.- error
An error object representing an error encountered in providing gyroscope data. If an error occurs, you should stop gyroscope updates and inform the user of the problem. If there is no error, this argument is
nil. Core Motion errors are of theCMErrorDomaindomain and theCMErrortype.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hCMMagnetometerHandler
The type of block callback for handling magnetometer data.
typedef void (^CMMagnetometerHandler)(CMMagnetometerData *magnetometerData, NSError *error);
Discussion
Blocks of type CMMagnetometerHandler are called when there is magnetometer data to process. You pass the block into the startMagnetometerUpdatesToQueue:withHandler: method as the second argument. Blocks of this type return no value but take two arguments:
- magnetometerData
An object that encapsulates a
CMMagneticFieldstructure with fields holding magnetic-field values for the three axes of movement.- error
An error object representing an error encountered in providing magnetometer data. If an error occurs, you should stop magnetometer updates and inform the user of the problem. If there is no error, this argument is
nil. Core Motion errors are of theCMErrorDomaindomain and theCMErrortype.
Availability
- Available in iOS 5.0 and later.
Declared In
CMMotionManager.hCMDeviceMotionHandler
The type of block callback for handling device-motion data.
typedef void (^CMDeviceMotionHandler)(CMDeviceMotion *motion, NSError *error);
Discussion
Blocks of type CMDeviceMotionHandler are called when there is device-motion data to process. You pass the block into startDeviceMotionUpdatesToQueue:withHandler: as the second argument. Blocks of this type return no value but take two arguments:
- motion
A
CMDeviceMotionobject, which encapsulates other objects and a structure representing attitude, rotation rate, gravity, and user acceleration.- error
An error object representing an error encountered in providing gyroscope data. If an error occurs, you should stop gyroscope updates and inform the user of the problem. If there is no error, this argument is
nil. Core Motion errors are of theCMErrorDomaindomain and theCMErrortype.
Availability
- Available in iOS 4.0 and later.
Declared In
CMMotionManager.hCore Motion Error Domain
The error domain for Core Motion.
extern NSString *const CMErrorDomain;
Constants
CMErrorDomainIdentifies the domain of
NSErrorobjects returned from Core Motion.Available in iOS 4.0 and later.
Declared in
CMErrorDomain.h.
Declared In
CMErrorDomain.hCMError
The type for Core Motion errors.
typedef enum {
CMErrorNULL = 100,
CMErrorDeviceRequiresMovement,
CMErrorTrueNorthNotAvailable
} CMError;
Constants
CMErrorNULLNo error.
Available in iOS 4.0 and later.
Declared in
CMError.h.CMErrorDeviceRequiresMovementThe device must move for a sampling of motion data to occur.
Available in iOS 5.0 and later.
Declared in
CMError.h.CMErrorTrueNorthNotAvailableTrue north is not available on this device. This usually indicates that the device’s location is not yet available.
Available in iOS 5.0 and later.
Declared in
CMError.h.
Declared In
CMError.h© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-10-12)