$npx -y skills add dpearson2699/swift-ios-skills --skill dockkitControl motorized camera docks and enable intelligent subject tracking using DockKit. Use when discovering DockKit-compatible accessories, implementing camera subject tracking for faces or bodies, controlling dock motors for pan and tilt, configuring framing behavior, setting reg
| 1 | # DockKit |
| 2 | |
| 3 | Framework for integrating with motorized camera stands and gimbals that |
| 4 | physically track subjects by rotating the iPhone. DockKit handles motor |
| 5 | control, subject detection, and framing so camera apps get 360-degree pan |
| 6 | and 90-degree tilt tracking with no additional code. Apps can override |
| 7 | system tracking to supply custom observations, control motors directly, |
| 8 | or adjust framing. iOS 17+, Swift 6.3. |
| 9 | |
| 10 | ## Contents |
| 11 | |
| 12 | - [Setup](#setup) |
| 13 | - [Discovering Accessories](#discovering-accessories) |
| 14 | - [System Tracking](#system-tracking) |
| 15 | - [Custom Tracking](#custom-tracking) |
| 16 | - [Framing and Region of Interest](#framing-and-region-of-interest) |
| 17 | - [Motor Control](#motor-control) |
| 18 | - [Animations](#animations) |
| 19 | - [Tracking State and Subject Selection](#tracking-state-and-subject-selection) |
| 20 | - [Accessory Events](#accessory-events) |
| 21 | - [Battery Monitoring](#battery-monitoring) |
| 22 | - [Common Mistakes](#common-mistakes) |
| 23 | - [Review Checklist](#review-checklist) |
| 24 | - [References](#references) |
| 25 | |
| 26 | ## Setup |
| 27 | |
| 28 | Import DockKit: |
| 29 | |
| 30 | ```swift |
| 31 | import DockKit |
| 32 | ``` |
| 33 | |
| 34 | DockKit requires a physical DockKit-compatible accessory and a real device. |
| 35 | The Simulator cannot connect to dock hardware. |
| 36 | |
| 37 | DockKit itself requires no special entitlements or DockKit-specific |
| 38 | Info.plist keys. Camera apps that use device cameras still need normal |
| 39 | camera privacy handling, including `NSCameraUsageDescription`. The framework |
| 40 | communicates with paired accessories automatically through the DockKit |
| 41 | system daemon. |
| 42 | |
| 43 | The app must use AVFoundation camera APIs. DockKit hooks into the camera |
| 44 | pipeline to analyze frames for system tracking. |
| 45 | |
| 46 | ## Discovering Accessories |
| 47 | |
| 48 | Use `DockAccessoryManager.shared` to observe dock connections: |
| 49 | |
| 50 | ```swift |
| 51 | import DockKit |
| 52 | |
| 53 | func observeAccessories() async throws { |
| 54 | for await stateChange in try DockAccessoryManager.shared.accessoryStateChanges { |
| 55 | switch stateChange.state { |
| 56 | case .docked: |
| 57 | guard let accessory = stateChange.accessory else { continue } |
| 58 | // Accessory is connected and ready |
| 59 | configureAccessory(accessory) |
| 60 | case .undocked: |
| 61 | // iPhone removed from dock |
| 62 | handleUndocked() |
| 63 | @unknown default: |
| 64 | break |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | ``` |
| 69 | |
| 70 | `accessoryStateChanges` emits `DockAccessory.StateChange` values with `state`, |
| 71 | `accessory`, and `trackingButtonEnabled`. Use `accessory.identifier` for the |
| 72 | name, category, and UUID; hardware details are available via `firmwareVersion` |
| 73 | and `hardwareModel`. |
| 74 | |
| 75 | ## System Tracking |
| 76 | |
| 77 | System tracking is DockKit's default mode. When enabled, the system |
| 78 | analyzes camera frames through built-in ML inference, detects faces and |
| 79 | bodies, and drives the motors to keep subjects in frame. Any app using |
| 80 | AVFoundation camera APIs benefits automatically. |
| 81 | |
| 82 | ### Enable or Disable |
| 83 | |
| 84 | ```swift |
| 85 | // Enable system tracking (default) |
| 86 | try await DockAccessoryManager.shared.setSystemTrackingEnabled(true) |
| 87 | |
| 88 | // Disable system tracking for custom control |
| 89 | try await DockAccessoryManager.shared.setSystemTrackingEnabled(false) |
| 90 | ``` |
| 91 | |
| 92 | System tracking state does not persist across app termination, reboots, |
| 93 | or background/foreground transitions. Set it explicitly whenever the app |
| 94 | needs a specific value. |
| 95 | |
| 96 | ### Tap to Select Subject |
| 97 | |
| 98 | Allow users to select a specific subject by tapping: |
| 99 | |
| 100 | ```swift |
| 101 | // Select the subject at a unit point in video-frame coordinates |
| 102 | try await accessory.selectSubject(at: CGPoint(x: 0.5, y: 0.5)) |
| 103 | |
| 104 | // Select specific subjects by identifier |
| 105 | try await accessory.selectSubjects([subjectUUID]) |
| 106 | |
| 107 | // Clear selection (return to automatic selection) |
| 108 | try await accessory.selectSubjects([]) |
| 109 | ``` |
| 110 | |
| 111 | ## Custom Tracking |
| 112 | |
| 113 | Disable system tracking and provide your own observations when using |
| 114 | custom ML models or the Vision framework. |
| 115 | |
| 116 | ### Providing Observations |
| 117 | |
| 118 | Construct `DockAccessory.Observation` values from your inference output |
| 119 | and pass them to the accessory at 10-30 fps: |
| 120 | |
| 121 | ```swift |
| 122 | import DockKit |
| 123 | import AVFoundation |
| 124 | |
| 125 | func processFrame( |
| 126 | _ sampleBuffer: CMSampleBuffer, |
| 127 | accessory: DockAccessory, |
| 128 | activeDevice: AVCaptureDevice |
| 129 | ) async throws { |
| 130 | let cameraInfo = DockAccessory.CameraInformation( |
| 131 | captureDevice: activeDevice.deviceType, |
| 132 | cameraPosition: activeDevice.position, |
| 133 | orientation: .corrected, |
| 134 | cameraIntrinsics: frameIntrinsics(from: sampleBuffer), |
| 135 | referenceDimensions: frameDimensions(from: sampleBuffer) |
| 136 | ) |
| 137 | |
| 138 | let detection = try await detector.detect(sampleBuffer) |
| 139 | let observationType: DockAccessory.Observation.ObservationType = switch detection.kind { |
| 140 | case .face: .humanFace |
| 141 | case .body: .humanBody |
| 142 | case .object: .object |
| 143 | } |
| 144 | |
| 145 | let observat |