CoreMotion
Read device motion, pedometer/activity, altitude, headphone, batched-workout, and submersion sensors with Core Motion. Scope: Swift 6.3, iOS 26+.
Contents
- Setup
- CMMotionManager: Sensor Data
- Processed Device Motion
- CMPedometer: Step and Distance Data
- CMMotionActivityManager: Activity Recognition
- CMAltimeter: Altitude Data
- Update Intervals and Battery
- Common Mistakes
- Review Checklist
- References
Setup
Info.plist
Add NSMotionUsageDescription to Info.plist with a user-facing string explaining
why your app needs motion data. Without this key, the app crashes on first access.
Authorization
Use the matching manager's authorizationStatus() or authorizationStatus
property when an API exposes one (CMPedometer, CMMotionActivityManager,
CMAltimeter, headphone motion, batched sensors, and submersion). Raw
CMMotionManager accelerometer/gyro/device-motion streams have no explicit
authorization request API; still ship the usage string and handle errors from
start/update callbacks.
CMMotionManager: Sensor Data
Create exactly one CMMotionManager per app. Multiple instances degrade
sensor update rates.
Accelerometer Updates
Gyroscope Updates
Polling Pattern (Games)
For games, start updates without a handler and poll the latest sample each frame:
Processed Device Motion
Device motion fuses accelerometer, gyroscope, and magnetometer into a single
CMDeviceMotion object with attitude, user acceleration (gravity removed),
rotation rate, and calibrated magnetic field.
When giving device-motion guidance, show the runtime frame check in the snippet
instead of hard-coding a corrected, magnetic-north, or true-north frame. Fall
back to .xArbitraryZVertical when the preferred frame is unavailable.
Attitude Reference Frames
For simple tilt controls, use .xArbitraryZVertical or
.xArbitraryCorrectedZVertical; they avoid magnetometer/location dependencies.
Before requesting corrected, magnetic-north, or true-north frames, call
CMMotionManager.availableAttitudeReferenceFrames() and fall back to an
available frame.
Check available frames before use:
CMPedometer: Step and Distance Data
CMPedometer provides step counts, distance, pace, cadence, and floor counts.
Availability Checks
CMMotionActivityManager: Activity Recognition
Detects whether the user is stationary, walking, running, cycling, or in a vehicle.
Historical Activity Query
CMAltimeter: Altitude Data
Altimeter access is covered by NSMotionUsageDescription; handle denied motion
access through unavailable data and update-handler errors.
Absolute altitude is altitude relative to sea level, not GPS-based altitude. First check availability. Absolute altitude is available only on supported hardware such as iPhone 12 or later and Apple Watch Series 6, Apple Watch SE, or later.
Update Intervals and Battery
Use the lowest frequency that meets your needs. Do not assume a fixed maximum
sample rate across devices. For high-frequency workout motion, use
CMBatchedSensorManager where supported and read its reported
accelerometerDataFrequency or deviceMotionDataFrequency instead of assigning
those read-only properties.
Common Mistakes
DON'T: Create multiple CMMotionManager instances
Retain one app-level CMMotionManager; competing instances can reduce update
rates.
DON'T: Skip sensor availability checks
Apply the matching is...Available gate immediately before starting each
sensor stream.
DON'T: Forget to stop updates
Pair every start with the matching stop in the counterpart lifecycle or task cancellation path.
DON'T: Use unnecessarily high update rates
Choose the lowest rate that meets the interaction and use the Update Intervals and Battery table as a starting point.
DON'T: Assume all CMMotionActivity properties are mutually exclusive
Review Checklist
-
NSMotionUsageDescriptionpresent in Info.plist with a clear explanation - Single
CMMotionManagerinstance shared across the app - Sensor availability checked before starting updates (
isAccelerometerAvailable, etc.) - Authorization status checked before pedometer/activity APIs
- Update interval set to the lowest acceptable frequency
- All
start*Updatescalls have matchingstop*Updatesin lifecycle counterparts - Handlers dispatched to appropriate queues (not blocking main for heavy processing)
-
CMMotionActivity.confidencechecked before acting on activity type - Error parameters checked in update handlers
- Device-motion snippets call
CMMotionManager.availableAttitudeReferenceFrames()before requesting a specific attitude frame - Attitude reference frame chosen based on actual need (not defaulting to true north unnecessarily)
References
- Extended patterns (SwiftUI integration, batched sensor manager, headphone motion, water submersion): references/motion-patterns.md [blocked]
- CoreMotion framework
- CMMotionManager
- CMPedometer
- CMMotionActivityManager
- CMDeviceMotion
- CMAltimeter
- CMAbsoluteAltitudeData
- CMBatchedSensorManager
- CMHeadphoneMotionManager
- CMWaterSubmersionManager
- Accessing submersion data
- Getting processed device-motion data


