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, t

By dpearson2699 · 3,217 installs

npx skills add dpearson2699/swift-ios-skills --skill core-motion

Source repository · Upstream listing

CoreMotion Read device motion, pedometer/activity, altitude, headphone, batched workout, and submersion sensors with Core Motion. Scope: Swift 6.3, iOS 26+. Contents [Setup]( setup) [CMMotionManager: Sensor Data]( cmmotionmanager sensor data) [Processed Device Motion]( processed device motion) [CMPedometer: Step and Distance Data]( cmpedometer step and distance data) [CMMotionActivityManager: Activity Recognition]( cmmotionactivitymanager activity recognition) [CMAltimeter: Altitude Data]( cmaltimeter altitude data) [Update Intervals and Battery]( update intervals and battery) [Common Mistakes]( common mistakes) [Review Checklist]( review checklist) [References]( 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. 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: Step and Distance Data CMPedometer provides step counts, distance, pace, cadence, and floor counts. Availability Checks Method What It Checks isStepCountingAvailable() Step counter hardware isDistanceAvailable() Distance estimation isFloorCountingAvailable() Barometric altimeter for floors isPaceAvailable() Pace data isCadenceAvailable() Cadence data 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 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. 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]( update intervals and battery) table as a starting point. DON'T: Assume all CMMotionActivity properties are mutually exclusive Review Checklist [ ] NSMotionUsageDescription present in Info.plist with a clear explanation [ ] Single CMMotionManager instance 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 Updates calls have matching stop Updates in lifecycle counterparts [ ] Handlers dispatched to appropriate queues (not blocking main for heavy processing) [ ] CMMotionActivity.confidence checked 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](references/motion patterns.md) [CoreMotion framework](https://sosumi.ai/documentation/coremotion) [CMMotionManager](https://sosumi.ai/documentation/coremotion/cmmotionmanager) [CMPedometer](https://sosumi.ai/documentation/coremotion/cmpedometer) [CMMotionActivityManager](https://sosumi.ai/documentation/coremotion/cmmotionactivitymanager) [CMDeviceMotion](https://sosumi.ai/documentation/coremotion/cmdevicemotion) [CMAltimeter](https://sosumi.ai/documentation/coremotion/cmaltimeter) [CMAbsoluteAltitudeData](https://sosumi.ai/documentation/coremotion/cmabsolutealtitudedata) [CMBatchedSensorManager](https://sosumi.ai/documentation/coremotion/cmbatchedsensormanager) [CMHeadphoneMotionManager](https://sosumi.ai/documentation/coremotion/cmheadphonemotionmanager) [CMWaterSubmersionManager](https://sosumi.ai/documentation/coremotion/cmwatersubmersionmanager) [Accessing submersion data](https://sosumi.ai/documentation/coremotion/accessing submersion data) [Getting processed device motion data](https://sosumi.ai/documentation/coremotion/getting processed device motion data)