$npx -y skills add dpearson2699/swift-ios-skills --skill sensorkitAccess research-grade sensor data using SensorKit for approved studies. Use when an app needs SensorKit entitlement setup, Research Sensor & Usage Data authorization, ambient light, recorded motion, device usage, keyboard metrics, visits, speech, face, wrist temperature, ECG, PPG
| 1 | # SensorKit |
| 2 | |
| 3 | Choose the exact `SRSensor` and verify its individual availability. Use |
| 4 | CoreMotion for ordinary motion/activity features and HealthKit for health |
| 5 | records and workouts. |
| 6 | |
| 7 | ## Contents |
| 8 | |
| 9 | - [Overview and Requirements](#overview-and-requirements) |
| 10 | - [Entitlements](#entitlements) |
| 11 | - [Info.plist Configuration](#infoplist-configuration) |
| 12 | - [Authorization](#authorization) |
| 13 | - [Available Sensors](#available-sensors) |
| 14 | - [SRSensorReader](#srsensorreader) |
| 15 | - [Recording and Fetching Data](#recording-and-fetching-data) |
| 16 | - [SRDevice](#srdevice) |
| 17 | - [Common Mistakes](#common-mistakes) |
| 18 | - [Review Checklist](#review-checklist) |
| 19 | - [References](#references) |
| 20 | |
| 21 | ## Overview and Requirements |
| 22 | |
| 23 | SensorKit enables research apps to record and fetch sensor data across iPhone |
| 24 | and Apple Watch. The framework requires: |
| 25 | |
| 26 | 1. **Apple-approved research study** -- submit a proposal at |
| 27 | [researchandcare.org](https://www.researchandcare.org/resources/accessing-sensorkit-data/). |
| 28 | 2. **SensorKit entitlement** -- Apple grants `com.apple.developer.sensorkit.reader.allow` |
| 29 | only for approved studies. |
| 30 | 3. **Manual provisioning profile** -- Xcode requires an explicit App ID with the |
| 31 | SensorKit capability enabled. |
| 32 | 4. **User authorization** -- the system presents a Research Sensor & Usage Data |
| 33 | sheet that users approve per-sensor. |
| 34 | 5. **Delayed retrieval** -- design fetch timing around the canonical |
| 35 | [Data Holding Period](#data-holding-period). |
| 36 | |
| 37 | An app can access up to 7 days of prior recorded data for an active sensor. |
| 38 | |
| 39 | ## Entitlements |
| 40 | |
| 41 | Add the SensorKit reader entitlement to a `.entitlements` file. List only the |
| 42 | sensors Apple approved for the study. Common entitlement values include: |
| 43 | |
| 44 | ```xml |
| 45 | <key>com.apple.developer.sensorkit.reader.allow</key> |
| 46 | <array> |
| 47 | <string>ambient-light-sensor</string> |
| 48 | <string>motion-accelerometer</string> |
| 49 | <string>device-usage</string> |
| 50 | <string>keyboard-metrics</string> |
| 51 | </array> |
| 52 | ``` |
| 53 | |
| 54 | Load the [Entitlement and Usage-Detail Catalog](references/sensorkit-patterns.md#entitlement-and-usage-detail-catalog) |
| 55 | when selecting the exact entitlement string and `NSSensorKitUsageDetail` key |
| 56 | for each approved sensor. Recheck specialized sensors against their individual |
| 57 | `SRSensor` pages. |
| 58 | |
| 59 | For manual signing, set Code Signing Entitlements to the entitlements file, |
| 60 | Code Signing Identity to `Apple Developer`, Code Signing Style to `Manual`, |
| 61 | and Provisioning Profile to the explicit profile with SensorKit capability. |
| 62 | |
| 63 | ## Info.plist Configuration |
| 64 | |
| 65 | Three keys are required: |
| 66 | |
| 67 | ```xml |
| 68 | <!-- Study purpose shown in the authorization sheet --> |
| 69 | <key>NSSensorKitUsageDescription</key> |
| 70 | <string>This study monitors activity patterns for sleep research.</string> |
| 71 | |
| 72 | <!-- Link to your study's privacy policy --> |
| 73 | <key>NSSensorKitPrivacyPolicyURL</key> |
| 74 | <string>https://example.com/privacy-policy</string> |
| 75 | |
| 76 | <!-- Per-sensor usage explanations --> |
| 77 | <key>NSSensorKitUsageDetail</key> |
| 78 | <dict> |
| 79 | <key>SRSensorUsageMotion</key> |
| 80 | <dict> |
| 81 | <key>Description</key> |
| 82 | <string>Measures physical activity levels during the study.</string> |
| 83 | <key>Required</key> |
| 84 | <true/> |
| 85 | </dict> |
| 86 | <key>SRSensorUsageAmbientLightSensor</key> |
| 87 | <dict> |
| 88 | <key>Description</key> |
| 89 | <string>Records ambient light to assess sleep environment.</string> |
| 90 | </dict> |
| 91 | </dict> |
| 92 | ``` |
| 93 | |
| 94 | If `Required` is `true` and the user denies that sensor, the system warns them |
| 95 | that the study needs it and offers a chance to reconsider. |
| 96 | |
| 97 | Use the exact usage-detail dictionary for each requested sensor. Load the |
| 98 | [Entitlement and Usage-Detail Catalog](references/sensorkit-patterns.md#entitlement-and-usage-detail-catalog) |
| 99 | when mapping sensors beyond the motion and ambient-light examples above. |
| 100 | |
| 101 | ## Authorization |
| 102 | |
| 103 | Request authorization for the sensors your study needs. The system shows the |
| 104 | Research Sensor & Usage Data sheet on first request. |
| 105 | |
| 106 | ```swift |
| 107 | import SensorKit |
| 108 | |
| 109 | let reader = SRSensorReader(sensor: .ambientLightSensor) |
| 110 | |
| 111 | // Request authorization for multiple sensors at once |
| 112 | SRSensorReader.requestAuthorization( |
| 113 | sensors: [.ambientLightSensor, .accelerometer, .keyboardMetrics] |
| 114 | ) { error in |
| 115 | if let error { |
| 116 | print("Authorization request failed: \(error)") |
| 117 | } |
| 118 | } |
| 119 | ``` |
| 120 | |
| 121 | Use one status handler both for the initial check and delegate changes: |
| 122 | |
| 123 | ```swift |
| 124 | private func applyAuthorizationStatus( |
| 125 | _ status: SRAuthorizationStatus, |
| 126 | to reader: SRSensorReader |
| 127 | ) { |
| 128 | switch status { |
| 129 | case .authorized: |
| 130 | reader.startRecording() |
| 131 | case .denied: |
| 132 | reader.stopRecording() |
| 133 | // Direct the user to Settings > Privacy > Research Sensor & U |