npx skills add ...
npx skills add dpearson2699/swift-ios-skills --skill core-motion
Access Core Motion accelerometer, gyroscope, magnetometer, device-motion, pedometer, activity-recognition, altitude, headphone motion, batched high-frequency workout motion, and water-submersion/depth data. Use when reading device sensors, counting steps, detecting walking/running/driving/cycling, tracking altitude, building motion interactions, handling AirPods head tracking, or implementing watchOS dive/depth features.
npx skills add dpearson2699/swift-ios-skills --skill core-motion
Read device motion, pedometer/activity, altitude, headphone, batched-workout, and submersion sensors with Core Motion. Scope: Swift 6.3, iOS 26+.
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.
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.
Create exactly one CMMotionManager per app. Multiple instances degrade
sensor update rates.
For games, start updates without a handler and poll the latest sample each frame:
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.
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.
| Frame | Use Case |
|---|---|
.xArbitraryZVertical | Default. Z is vertical, X arbitrary at start. Most games. |
.xArbitraryCorrectedZVertical | Same as above, corrected for gyro drift over time. |
.xMagneticNorthZVertical | X points to magnetic north. Requires magnetometer. |
.xTrueNorthZVertical | X points to true north. Requires magnetometer + location. |
Check available frames before use:
CMPedometer provides step counts, distance, pace, cadence, and floor counts.
| Method | What It Checks |
|---|---|
isStepCountingAvailable() | Step counter hardware |
isDistanceAvailable() | Distance estimation |
isFloorCountingAvailable() | Barometric altimeter for floors |
isPaceAvailable() | Pace data |
isCadenceAvailable() | Cadence data |
Detects whether the user is stationary, walking, running, cycling, or in a vehicle.
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.
| Interval | Hz | Use Case | Battery Impact |
|---|---|---|---|
1.0 / 10.0 | 10 | UI orientation | Low |
1.0 / 30.0 | 30 | Casual games | Moderate |
1.0 / 60.0 | 60 | Action games | High |
1.0 / 100.0 | 100 | Max rate (iPhone) | Very High |
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.
Retain one app-level CMMotionManager; competing instances can reduce update
rates.
Apply the matching is...Available gate immediately before starting each
sensor stream.
Pair every start with the matching stop in the counterpart lifecycle or task cancellation path.
Choose the lowest rate that meets the interaction and use the Update Intervals and Battery table as a starting point.
NSMotionUsageDescription present in Info.plist with a clear explanationCMMotionManager instance shared across the appisAccelerometerAvailable, etc.)start*Updates calls have matching stop*Updates in lifecycle counterpartsCMMotionActivity.confidence checked before acting on activity typeCMMotionManager.availableAttitudeReferenceFrames() before requesting a specific attitude frame