CMMotionActivityManager Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/CoreMotion.framework
Availability
Available in iOS 7.0 and later.
Declared in
CMMotionActivityManager.h

Overview

The CMMotionActivityManager class provides access to the motion data stored by a device. Motion data reflects whether the user is walking, running, in a vehicle, or stationary for periods of time. A navigation app might look for changes in the current type of motion and offer different directions for each. Using this class, you can ask for notifications when the current type of motion changes or you can gather past motion change data.

Tasks

Determining Activity Availability

Starting and Stopping Activity Updates

Getting Historical Activity Data

Class Methods

isActivityAvailable

Returns a Boolean indicating whether motion data is available on the current device.

+ (BOOL)isActivityAvailable
Return Value

YES if motion data is available or NO if it is not.

Discussion

Motion data is not available on all iOS devices. Use this method to determine if support is available on the current device.

Availability
  • Available in iOS 7.0 and later.
Declared In
CMMotionActivityManager.h

Instance Methods

queryActivityStartingFromDate:toDate:toQueue:withHandler:

Gathers and returns historical motion data for the specified time period

- (void)queryActivityStartingFromDate:(NSDate *)start toDate:(NSDate *)end toQueue:(NSOperationQueue *)queue withHandler:(CMMotionActivityQueryHandler)handler
Parameters
start

The start time to use when gathering motion data. This parameter must not be nil.

end

The end time to use when gathering motion data. This parameter must not be nil.

queue

The operation queue on which to execute the specified handler block. You can specify a custom queue or use the operation queue associated with your app’s main thread. This parameter must not be nil.

handler

The block to execute with the results. For information about the parameters of this block, see CMMotionActivityQueryHandler. This parameter must not be nil.

Discussion

This method runs asynchronously, returning immediately and delivering the results to the specified handler block. The system stores only the last seven days worth of activity data at most. If there are no samples for the specified range of time, an error object with the code CMErrorUnknown is passed to the handler block.

Availability
  • Available in iOS 7.0 and later.
Declared In
CMMotionActivityManager.h

startActivityUpdatesToQueue:withHandler:

Starts the delivery of current motion data updates to your app.

- (void)startActivityUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMMotionActivityHandler)handler
Parameters
queue

The operation queue on which to execute the specified handler block. You can specify a custom queue or use the operation queue associated with your app’s main thread. This parameter must not be nil.

handler

The block to execute when a change in the current type of motion is detected. For information about the parameters of this block, see CMMotionActivityHandler. This property must not be nil.

Discussion

This method initiates the tracking of motion data asynchronously. Upon calling this method, the motion activity manager executes the handler block on the specified queue, reporting the current motion in effect for the device. After that, the motion activity manager executes the handler block only when the motion data changes.

The handler block is executed on a best effort basis and updates are not delivered while your app is suspended. If updates arrived while your app was suspended, the last update is delivered to your app when it resumes execution. To get all of the updates that occurred while your app was suspended, use the queryActivityStartingFromDate:toDate:toQueue:withHandler: method.

Upon starting activity updates, the motion activity manager reports changes to its block until you call the stopActivityUpdates method. If you call this method again with a new block, the activity manager stops delivering updates to the old block and delivers them to the new block instead. Updates stop altogether when the motion activity manager object itself is deallocated.

Availability
  • Available in iOS 7.0 and later.
Declared In
CMMotionActivityManager.h

stopActivityUpdates

Stops the delivery of motion updates to your app

- (void)stopActivityUpdates
Discussion

Call this method to stop the delivery of updates that you started by calling the startActivityUpdatesToQueue:withHandler: method. This method does not stop queries started using the queryActivityStartingFromDate:toDate:toQueue:withHandler: method.

Availability
  • Available in iOS 7.0 and later.
Declared In
CMMotionActivityManager.h

Constants

CMMotionActivityHandler

A block that reports the current motion associated with the device.

typedef void (^CMMotionActivityHandler)(CMMotionActivity *activity)
Discussion

This block takes the following parameter:

activity

The motion activity object that defines the current type of motion for the device.

Availability
  • Available in iOS 7.0 and later.
Declared In
CMMotionActivityManager.h

CMMotionActivityQueryHandler

A block that reports the motion updates that occurred between the specified query interval.

typedef void (^CMMotionActivityQueryHandler)(NSArray *activities, NSError *error)
Discussion

This block takes the following parameters:

activities

An array of CMMotionActivity objects indicating the updates that occurred. The objects in the array are ordered by the time at which they occurred in the specified time interval. Use the startDate property in each motion object to determine when the update occurred.

error

An error object indicating that there was a problem gathering the data or nil if the motion data was determined correctly.

Availability
  • Available in iOS 7.0 and later.
Declared In
CMMotionActivityManager.h