Capture motion in iOS apps

Forward UIKit shakes and app-owned Core Motion samples into an Ansight session.

Motion capture is opt-in. The aggregate Ansight SwiftPM product and CocoaPod include AnsightMotion; a core-only integration can add that product or pod separately. Ansight does not create a CMMotionManager or request motion permissions. Your app decides whether and how to observe the device.

Enable capture

Add withMotionCapture() to the options builder at app startup:

import Ansight

try AnsightRuntime.shared.initializeAndActivateAnsightSdk { options in
    options.withMotionCapture()
}

Forward UIKit shakes

A UIKit responder that receives shake events can forward them directly:

import UIKit
import AnsightMotion

final class ShakeViewController: UIViewController {
    override var canBecomeFirstResponder: Bool { true }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        becomeFirstResponder()
    }

    override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        if motion == .motionShake {
            AnsightMotion.recordShake()
        }
        super.motionEnded(motion, with: event)
    }
}

The view or controller must participate in the responder chain to receive the event. Forwarding a UIKit shake does not start Core Motion.

Forward accelerometer samples

If your app already receives CMAccelerometerData, convert its acceleration from g to metres per second squared before forwarding:

import CoreMotion
import AnsightMotion

func forwardAccelerometer(_ data: CMAccelerometerData) {
    let metresPerSecondSquared = 9.80665
    AnsightMotion.recordAccelerometer(
        x: data.acceleration.x * metresPerSecondSquared,
        y: data.acceleration.y * metresPerSecondSquared,
        z: data.acceleration.z * metresPerSecondSquared
    )
}

withMotionCapture() enables both event types by default. Pass AnsightMotionCaptureOptions to disable either type or set minimumSampleIntervalMilliseconds; the interval defaults to 20 ms and is clamped to 10–1000 ms. Calls while the runtime is inactive or capture is disabled are ignored.

The host stores app-observed motion in motion/events.json and the session’s motion timeline. For simulator-injected input and the shared evidence contract, see Motion Capture.