sensorkit
dpearson2699/swift-ios-skills
Access research-grade sensor data from iOS/watchOS for Apple-approved studies.
What is sensorkit?
SensorKit provides access to ambient light, motion, device usage, keyboard metrics, visits, speech, face, wrist temperature, ECG, PPG, and sleep data for approved research studies. Use only when your app has Apple's SensorKit entitlement and users grant Research Sensor & Usage Data authorization.
- Collect ambient light, accelerometer, gyroscope, and pressure sensor data
- Access device usage, keyboard metrics, and app activity (messages, phone calls)
- Record heart rate, ECG, PPG, and wrist temperature from Apple Watch
- Capture location visits, face metrics, and speech metrics from Siri/telephony
- Retrieve sleep sessions and on-wrist detection state
- Access up to 7 days of prior recorded data with 24-hour hold on new recordings
How to install sensorkit
npx skills add https://github.com/dpearson2699/swift-ios-skills --skill sensorkit- Apple-approved research study proposal submitted to researchandcare.org
- SensorKit entitlement (com.apple.developer.sensorkit.reader.allow) granted by Apple
- Manual provisioning profile with SensorKit capability enabled
- Explicit App ID configured in Apple Developer account
- Swift 6.3 and iOS 26+ (or watchOS equivalent)
How to use sensorkit
- 1.Add SensorKit entitlement values to .entitlements file for each approved sensor
- 2.Configure Info.plist with NSSensorKitUsageDescription, NSSensorKitPrivacyPolicyURL, and NSSensorKitUsageDetail entries
- 3.Request authorization using SRSensorReader.requestAuthorization() for needed sensors
- 4.Check authorizationStatus before calling startRecording()
- 5.Fetch recorded data using SRSensorReader, accounting for 24-hour hold on new data
- 6.Route ordinary motion to CoreMotion and health records to HealthKit instead of SensorKit
Use cases
- Sleep and circadian rhythm research studies tracking motion and light exposure
- Cardiovascular health studies collecting ECG and PPG data from wearables
- Behavioral research monitoring device usage patterns and app interactions
- Accessibility studies analyzing keyboard metrics and acoustic settings
- Location-based studies tracking user visits and movement patterns
- Research institutions with Apple-approved studies
- Health and wellness researchers
- Behavioral science teams
- Accessibility researchers
- Epidemiological study coordinators
sensorkit FAQ
No. SensorKit is restricted to Apple-approved research studies only. You must submit a research proposal and receive the com.apple.developer.sensorkit.reader.allow entitlement before accessing any sensor data.
The 24-hour hold gives users time to review and delete data they do not want to share with researchers before it becomes accessible to the app.
No. Use CoreMotion for ordinary accelerometer, gyroscope, pedometer, and activity recognition. Use HealthKit for health records and workout data. SensorKit is only for research-grade sensor collection in approved studies.
The system warns the user that the study needs that sensor and offers a chance to reconsider. If they continue to deny, the sensor remains inaccessible and you cannot record that data.
You can access up to 7 days of prior recorded data for an active sensor, plus newly recorded data after the 24-hour hold period expires.
Full instructions (SKILL.md)
Source of truth, from dpearson2699/swift-ios-skills.
name: sensorkit description: "Access 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, acoustic settings, or sleep-session data. Route ordinary motion to CoreMotion and health records/workouts to HealthKit."
SensorKit
Collect research-grade sensor data from iOS and watchOS devices for approved research studies. SensorKit provides access to ambient light, motion, device usage, keyboard metrics, visits, phone/messaging usage, speech metrics, face metrics, wrist temperature, heart rate, ECG, and PPG data. Targets Swift 6.3 / iOS 26+.
SensorKit is restricted to Apple-approved research studies. Apps must submit
a research proposal to Apple and receive the com.apple.developer.sensorkit.reader.allow
entitlement before any sensor data is accessible. This is not a general-purpose
sensor API -- use CoreMotion for ordinary accelerometer, gyroscope, pedometer,
or activity-recognition features, and HealthKit for health records and workouts.
Contents
- Overview and Requirements
- Entitlements
- Info.plist Configuration
- Authorization
- Available Sensors
- SRSensorReader
- Recording and Fetching Data
- SRDevice
- Common Mistakes
- Review Checklist
- References
Overview and Requirements
SensorKit enables research apps to record and fetch sensor data across iPhone and Apple Watch. The framework requires:
- Apple-approved research study -- submit a proposal at researchandcare.org.
- SensorKit entitlement -- Apple grants
com.apple.developer.sensorkit.reader.allowonly for approved studies. - Manual provisioning profile -- Xcode requires an explicit App ID with the SensorKit capability enabled.
- User authorization -- the system presents a Research Sensor & Usage Data sheet that users approve per-sensor.
- 24-hour data hold -- newly recorded data is inaccessible for 24 hours, giving users time to delete data they do not want to share.
An app can access up to 7 days of prior recorded data for an active sensor.
Entitlements
Add the SensorKit reader entitlement to a .entitlements file. List only the
sensors Apple approved for the study. Common entitlement values include:
<key>com.apple.developer.sensorkit.reader.allow</key>
<array>
<string>ambient-light-sensor</string>
<string>motion-accelerometer</string>
<string>motion-rotation-rate</string>
<string>device-usage</string>
<string>keyboard-metrics</string>
<string>messages-usage</string>
<string>phone-usage</string>
<string>visits</string>
<string>pedometer</string>
<string>on-wrist</string>
<string>speech-metrics-siri</string>
<string>speech-metrics-telephony</string>
<string>ambient-pressure</string>
<string>ecg</string>
<string>ppg</string>
</array>
Verify newer or specialized sensors against their individual SRSensor pages.
For example, Apple's ECG and PPG sensor pages explicitly require ecg and
ppg entitlement values in addition to their NSSensorKitUsageDetail entries.
For manual signing, set Code Signing Entitlements to the entitlements file,
Code Signing Identity to Apple Developer, Code Signing Style to Manual,
and Provisioning Profile to the explicit profile with SensorKit capability.
Info.plist Configuration
Three keys are required:
<!-- Study purpose shown in the authorization sheet -->
<key>NSSensorKitUsageDescription</key>
<string>This study monitors activity patterns for sleep research.</string>
<!-- Link to your study's privacy policy -->
<key>NSSensorKitPrivacyPolicyURL</key>
<string>https://example.com/privacy-policy</string>
<!-- Per-sensor usage explanations -->
<key>NSSensorKitUsageDetail</key>
<dict>
<key>SRSensorUsageMotion</key>
<dict>
<key>Description</key>
<string>Measures physical activity levels during the study.</string>
<key>Required</key>
<true/>
</dict>
<key>SRSensorUsageAmbientLightSensor</key>
<dict>
<key>Description</key>
<string>Records ambient light to assess sleep environment.</string>
</dict>
</dict>
If Required is true and the user denies that sensor, the system warns them
that the study needs it and offers a chance to reconsider.
Use the exact usage-detail dictionary for each requested sensor. Examples:
motion sensors use SRSensorUsageMotion, ambient pressure uses SRSensorUsageElevation,
ECG uses SRSensorUsageECG, PPG uses SRSensorUsagePPG, heart rate uses
SRSensorUsageHeartRate, and wrist temperature uses SRSensorUsageWristTemperature.
Authorization
Request authorization for the sensors your study needs. The system shows the Research Sensor & Usage Data sheet on first request.
import SensorKit
let reader = SRSensorReader(sensor: .ambientLightSensor)
// Request authorization for multiple sensors at once
SRSensorReader.requestAuthorization(
sensors: [.ambientLightSensor, .accelerometer, .keyboardMetrics]
) { error in
if let error {
print("Authorization request failed: \(error)")
}
}
Check a reader's current status before recording:
switch reader.authorizationStatus {
case .authorized:
reader.startRecording()
case .denied:
// User declined -- direct to Settings > Privacy > Research Sensor & Usage Data
break
case .notDetermined:
// Request authorization first
break
@unknown default:
break
}
Monitor status changes through the delegate:
func sensorReader(_ reader: SRSensorReader, didChange authorizationStatus: SRAuthorizationStatus) {
switch authorizationStatus {
case .authorized:
reader.startRecording()
case .denied:
reader.stopRecording()
default:
break
}
}
Available Sensors
Device Sensors
| Sensor | Type | Sample Type |
|---|---|---|
.deviceUsageReport | Device usage | SRDeviceUsageReport |
.keyboardMetrics | Keyboard activity | SRKeyboardMetrics |
.onWristState | Watch wrist state | SRWristDetection |
.acousticSettings | Acoustic/accessibility settings | SRAcousticSettings |
App Activity Sensors
| Sensor | Type | Sample Type |
|---|---|---|
.messagesUsageReport | Messages app usage | SRMessagesUsageReport |
.phoneUsageReport | Phone call usage | SRPhoneUsageReport |
User Activity Sensors
| Sensor | Type | Sample Type |
|---|---|---|
.accelerometer | Acceleration data | [CMRecordedAccelerometerData] |
.rotationRate | Rotation rate | [CMRecordedRotationRateData] |
.pedometerData | Step/distance data | CMPedometerData |
.visits | Visited locations | SRVisit |
.mediaEvents | Media interactions | SRMediaEvent |
.faceMetrics | Face expressions | SRFaceMetrics |
.heartRate | Heart rate | CMHighFrequencyHeartRateData |
.odometer | Speed/slope | CMOdometerData |
.siriSpeechMetrics | Siri speech | SRSpeechMetrics |
.telephonySpeechMetrics | Phone speech | SRSpeechMetrics |
.wristTemperature | Wrist temp (sleep) | SRWristTemperatureSession |
.sleepSessions | Sleep session summaries | SRSleepSession |
.photoplethysmogram | PPG stream | [SRPhotoplethysmogramSample] |
.electrocardiogram | ECG stream | [SRElectrocardiogramSample] |
Environment Sensors
| Sensor | Type | Sample Type |
|---|---|---|
.ambientLightSensor | Ambient light | SRAmbientLightSample |
.ambientPressure | Pressure/temp | [CMRecordedPressureData] |
SRSensorReader
SRSensorReader is the central class for accessing sensor data. Each instance
reads from a single sensor.
import SensorKit
// Create a reader for one sensor
let lightReader = SRSensorReader(sensor: .ambientLightSensor)
let keyboardReader = SRSensorReader(sensor: .keyboardMetrics)
// Assign delegate to receive callbacks
lightReader.delegate = self
keyboardReader.delegate = self
The reader communicates entirely through SRSensorReaderDelegate:
| Delegate Method | Purpose |
|---|---|
sensorReader(_:didChange:) | Authorization status changed |
sensorReaderWillStartRecording(_:) | Recording is about to start |
sensorReader(_:startRecordingFailedWithError:) | Recording failed to start |
sensorReaderDidStopRecording(_:) | Recording stopped |
sensorReader(_:stopRecordingFailedWithError:) | Recording failed to stop |
sensorReader(_:didFetch:) | Devices fetched |
sensorReader(_:fetchDevicesDidFailWithError:) | Device fetch failed |
sensorReader(_:fetching:didFetchResult:) | Sample received |
sensorReader(_:didCompleteFetch:) | Fetch completed |
sensorReader(_:fetching:failedWithError:) | Fetch failed |
Recording and Fetching Data
Start and Stop Recording
// Begin recording -- sensor stays active as long as any app has a stake
reader.startRecording()
// Stop recording -- framework deactivates the sensor when
// no app or system process is using it
reader.stopRecording()
Fetch Data
Build an SRFetchRequest with a time range and target device, then pass it to
the reader:
let request = SRFetchRequest()
request.device = SRDevice.current
request.from = SRAbsoluteTime(CFAbsoluteTimeGetCurrent() - 86400 * 2) // 2 days ago
request.to = SRAbsoluteTime.current()
reader.fetch(request)
Receive results through the delegate:
func sensorReader(
_ reader: SRSensorReader,
fetching request: SRFetchRequest,
didFetchResult result: SRFetchResult<AnyObject>
) -> Bool {
let timestamp = result.timestamp
switch reader.sensor {
case .ambientLightSensor:
if let sample = result.sample as? SRAmbientLightSample {
let lux = sample.lux
let chromaticity = sample.chromaticity
let placement = sample.placement
processSample(lux: lux, chromaticity: chromaticity, at: timestamp)
}
case .keyboardMetrics:
if let sample = result.sample as? SRKeyboardMetrics {
let words = sample.totalWords
let speed = sample.typingSpeed
processKeyboard(words: words, speed: speed, at: timestamp)
}
case .deviceUsageReport:
if let sample = result.sample as? SRDeviceUsageReport {
let wakes = sample.totalScreenWakes
let unlocks = sample.totalUnlocks
processUsage(wakes: wakes, unlocks: unlocks, at: timestamp)
}
default:
break
}
return true // Return true to continue receiving results
}
func sensorReader(_ reader: SRSensorReader, didCompleteFetch request: SRFetchRequest) {
print("Fetch complete for \(reader.sensor)")
}
func sensorReader(
_ reader: SRSensorReader,
fetching request: SRFetchRequest,
failedWithError error: any Error
) {
print("Fetch failed: \(error)")
}
Cast result.sample to the sample shape for the reader's sensor. Some streams
return one object per result, while recorded motion, ECG, PPG, and ambient
pressure streams can return arrays of recorded samples.
Data Holding Period
SensorKit imposes a 24-hour holding period on newly recorded data. Fetch requests whose time range overlaps this period return no results. Design data collection workflows around this delay.
SRDevice
SRDevice identifies the hardware source for sensor samples. Use it to
distinguish data from iPhone versus Apple Watch.
// Get the current device
let currentDevice = SRDevice.current
print("Model: \(currentDevice.model)")
print("System: \(currentDevice.systemName) \(currentDevice.systemVersion)")
// Fetch all available devices for a sensor
reader.fetchDevices()
Handle fetched devices through the delegate:
func sensorReader(_ reader: SRSensorReader, didFetch devices: [SRDevice]) {
for device in devices {
let request = SRFetchRequest()
request.device = device
request.from = SRAbsoluteTime(CFAbsoluteTimeGetCurrent() - 86400)
request.to = SRAbsoluteTime.current()
reader.fetch(request)
}
}
func sensorReader(_ reader: SRSensorReader, fetchDevicesDidFailWithError error: any Error) {
print("Failed to fetch devices: \(error)")
}
SRDevice Properties
| Property | Type | Description |
|---|---|---|
model | String | User-defined device name |
name | String | Framework-defined device name |
systemName | String | OS name (iOS, watchOS) |
systemVersion | String | OS version |
productType | String | Hardware identifier |
current | SRDevice | Class property for the running device |
Common Mistakes
DON'T: Attempt to use SensorKit without the entitlement
// WRONG -- fails at runtime with SRError.invalidEntitlement
let reader = SRSensorReader(sensor: .ambientLightSensor)
reader.startRecording()
// CORRECT -- obtain entitlement from Apple first, configure manual
// provisioning profile, then use SensorKit
DON'T: Expect immediate data access
// WRONG -- fetching data recorded moments ago returns nothing
reader.startRecording()
// ... record for a few minutes ...
let request = SRFetchRequest()
request.from = SRAbsoluteTime(CFAbsoluteTimeGetCurrent() - 300)
request.to = SRAbsoluteTime.current()
reader.fetch(request) // Empty results due to 24-hour hold
// CORRECT -- fetch data that is at least 24 hours old
request.from = SRAbsoluteTime(CFAbsoluteTimeGetCurrent() - 86400 * 3)
request.to = SRAbsoluteTime(CFAbsoluteTimeGetCurrent() - 86400)
reader.fetch(request)
DON'T: Forget to set the delegate before fetching
// WRONG -- no delegate means no callbacks, results are silently lost
let reader = SRSensorReader(sensor: .accelerometer)
reader.startRecording()
reader.fetch(request)
// CORRECT -- assign delegate first
reader.delegate = self
reader.startRecording()
reader.fetch(request)
DON'T: Skip per-sensor Info.plist usage detail
// WRONG -- missing NSSensorKitUsageDetail for the sensor
// Authorization sheet shows no explanation, user is less likely to approve
// CORRECT -- add usage detail for every sensor you request
// See Info.plist Configuration section above
DON'T: Ignore SRError codes
// WRONG -- generic error handling
func sensorReader(_ reader: SRSensorReader, fetching: SRFetchRequest, failedWithError error: any Error) {
print("Error")
}
// CORRECT -- handle specific error codes
func sensorReader(_ reader: SRSensorReader, fetching: SRFetchRequest, failedWithError error: any Error) {
if let srError = error as? SRError {
switch srError.code {
case .invalidEntitlement:
// Entitlement missing or sensor not in entitlement array
break
case .noAuthorization:
// User has not authorized this sensor
break
case .dataInaccessible:
// Data in 24-hour holding period or otherwise unavailable
break
case .fetchRequestInvalid:
// Invalid time range or device
break
case .promptDeclined:
// User declined the authorization prompt
break
@unknown default:
break
}
}
}
Review Checklist
- Apple-approved research study in place before development
-
com.apple.developer.sensorkit.reader.allowentitlement lists only needed sensors - Manual provisioning profile with explicit App ID and SensorKit capability
-
NSSensorKitUsageDescriptionin Info.plist with clear study purpose -
NSSensorKitPrivacyPolicyURLin Info.plist with valid privacy policy URL -
NSSensorKitUsageDetailentries for every requested sensor -
Requiredkey set appropriately for essential vs. optional sensors - Authorization requested before recording, status checked before fetching
- Delegate assigned before calling
startRecording()orfetch(_:) - Fetch request time ranges account for 24-hour data holding period
-
SRErrorcodes handled in all failure delegate methods -
fetchDevices()used to discover available devices before fetching -
stopRecording()called when data collection is complete -
sensorReader(_:fetching:didFetchResult:)returnstrueto continue orfalseto stop
References
- Extended patterns (delegate wiring, multi-sensor manager, sample type details): references/sensorkit-patterns.md
- SensorKit framework
- SRSensorReader
- SRSensor
- SRDevice
- SRFetchRequest
- Configuring your project for sensor reading
- com.apple.developer.sensorkit.reader.allow
Related skills
More from dpearson2699/swift-ios-skills and the wider catalog.

shareplay-activities
Build shared real-time experiences with GroupActivities and SharePlay on iOS, macOS, tvOS, and visionOS.

speech-recognition
Transcribe speech to text on iOS using Apple's Speech framework with live microphone and file support.

spritekit
Build 2D games and animations with SpriteKit for iOS using scenes, sprites, physics, and SwiftUI integration.

storekit
Implement in-app purchases and subscriptions using StoreKit 2 on iOS 16+.

swift-api-design-guidelines
Apply Swift API Design Guidelines to name, label, and document Swift APIs. Covers argument label rules (prepositional phrase rule, grammatical phrase rule, first-label omission), mutating/nonmutating pair naming (-ed/-ing participle pattern, form- prefix, sort/sorted, formUnion/union), side-effect naming (noun for pure, verb for mutating), documentation comment structure (summary by declaration kind, O(1) complexity rule), clarity at call site, role-based naming, protocol naming (-able/-ible/-ing), default arguments over method families, casing conventions, and terminology. Use when designing new Swift APIs, reviewing naming and argument labels, writing documentation comments, or refactoring for call site clarity.

swift-architecture
Select, implement, or migrate between app architecture patterns for Apple platform apps. Use when choosing between MV (Model-View with @Observable), MVVM, MVI, TCA (The Composable Architecture), Clean Architecture, VIPER, or Coordinator patterns; when evaluating architecture fit for a feature's complexity; when migrating from one pattern to another; or when reviewing whether an app's current architecture is appropriate. Scoped to Apple-platform patterns using Swift 6.3, SwiftUI, and UIKit.