PluginBench
Skill
Pass
Audit score 90

sensorkit

dpearson2699/swift-ios-skills

Access research-grade sensor data (motion, light, keyboard, ECG, PPG, temperature) for Apple-approved studies.

What is sensorkit?

SensorKit enables research apps to record and fetch sensor data from iPhone and Apple Watch, including ambient light, motion, device usage, keyboard metrics, speech, face, wrist temperature, ECG, PPG, and sleep sessions. Use this skill when building a study app that requires SensorKit entitlement setup, authorization flows, and multi-sensor data collection within Apple's research framework.

  • Set up SensorKit entitlements and manual provisioning profiles for approved research studies
  • Configure Info.plist with study purpose, privacy policy, and per-sensor usage descriptions
  • Request and handle Research Sensor & Usage Data authorization for multiple sensors
  • Create SRSensorReader instances to record and fetch data from individual sensors
  • Build SRFetchRequest queries to retrieve up to 7 days of prior sensor data
  • Route ordinary motion to CoreMotion and health records to HealthKit instead of SensorKit

How to install sensorkit

npx skills add https://github.com/dpearson2699/swift-ios-skills --skill sensorkit
Prerequisites
  • 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
Claude Code
Cursor
Windsurf
Cline

How to use sensorkit

  1. 1.Create a .entitlements file listing only the sensors approved for your study (e.g., ambient-light-sensor, motion-accelerometer, device-usage, keyboard-metrics)
  2. 2.Configure Info.plist with NSSensorKitUsageDescription, NSSensorKitPrivacyPolicyURL, and NSSensorKitUsageDetail entries for each sensor
  3. 3.Set Xcode code signing to Manual with your explicit provisioning profile and entitlements file
  4. 4.Call SRSensorReader.requestAuthorization(sensors:) to present the Research Sensor & Usage Data sheet to users
  5. 5.Create SRSensorReader instances for each sensor, assign a delegate, and call startRecording()
  6. 6.Build SRFetchRequest with time range and device, then call reader.fetch() to retrieve historical data
  7. 7.Handle results in the delegate's sensorReader(_:fetching:didFetchResult:) method, casting samples to their specific types (SRAmbientLightSample, SRKeyboardMetrics, SRDeviceUsageReport, etc.)

Use cases

Good for
  • Sleep research study collecting ambient light, motion, and wrist temperature data from participants
  • Activity monitoring app tracking device usage patterns and keyboard metrics across multiple users
  • Clinical trial gathering ECG and PPG readings from Apple Watch for cardiovascular research
  • Longitudinal study recording speech and face data with proper consent and authorization flows
  • Workplace wellness program measuring screen time, unlocks, and activity levels with per-sensor user controls
Who it's for
  • Research app developers building studies approved by Apple's researchandcare.org program
  • Clinical researchers collecting sensor data from iPhone and Apple Watch participants
  • Health scientists designing longitudinal studies with multi-sensor data requirements
  • iOS engineers implementing proper authorization and data-fetching patterns for regulated studies

sensorkit FAQ

What's the difference between SensorKit and CoreMotion or HealthKit?

SensorKit is for research-approved studies and provides access to specialized sensors (ECG, PPG, wrist temperature, speech, face). Route ordinary motion/activity to CoreMotion and health records/workouts to HealthKit instead.

How long can I access historical sensor data?

An app can access up to 7 days of prior recorded data for an active sensor. Design your fetch timing around this Data Holding Period.

What happens if a user denies authorization for a required sensor?

If NSSensorKitUsageDetail marks a sensor as Required=true and the user denies it, the system warns them the study needs it and offers a chance to reconsider.

Do I need manual provisioning or can I use automatic signing?

SensorKit requires manual provisioning. Set Code Signing Style to Manual, use an explicit App ID with SensorKit capability, and configure the provisioning profile in Xcode.

How do I know which entitlement strings and usage-detail keys to use?

Load the Entitlement and Usage-Detail Catalog in the skill's references to map each SRSensor to its exact entitlement string and NSSensorKitUsageDetail key.

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

Choose the exact SRSensor and verify its individual availability. Use CoreMotion for ordinary motion/activity features and HealthKit for health records and workouts.

Contents

Overview and Requirements

SensorKit enables research apps to record and fetch sensor data across iPhone and Apple Watch. The framework requires:

  1. Apple-approved research study -- submit a proposal at researchandcare.org.
  2. SensorKit entitlement -- Apple grants com.apple.developer.sensorkit.reader.allow only for approved studies.
  3. Manual provisioning profile -- Xcode requires an explicit App ID with the SensorKit capability enabled.
  4. User authorization -- the system presents a Research Sensor & Usage Data sheet that users approve per-sensor.
  5. Delayed retrieval -- design fetch timing around the canonical Data Holding Period.

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>device-usage</string>
    <string>keyboard-metrics</string>
</array>

Load the Entitlement and Usage-Detail Catalog when selecting the exact entitlement string and NSSensorKitUsageDetail key for each approved sensor. Recheck specialized sensors against their individual SRSensor pages.

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. Load the Entitlement and Usage-Detail Catalog when mapping sensors beyond the motion and ambient-light examples above.

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)")
    }
}

Use one status handler both for the initial check and delegate changes:

private func applyAuthorizationStatus(
    _ status: SRAuthorizationStatus,
    to reader: SRSensorReader
) {
    switch status {
    case .authorized:
        reader.startRecording()
    case .denied:
        reader.stopRecording()
        // Direct the user to Settings > Privacy > Research Sensor & Usage Data.
    case .notDetermined:
        break // Request authorization first.
    @unknown default:
        break
    }
}

applyAuthorizationStatus(reader.authorizationStatus, to: reader)

func sensorReader(_ reader: SRSensorReader, didChange authorizationStatus: SRAuthorizationStatus) {
    applyAuthorizationStatus(authorizationStatus, to: reader)
}

Available Sensors

Load the Sensor Catalog to map each SRSensor to its sample type. Request only sensors approved for the study and recheck the selected sensor's availability and usage-detail key.

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 through SRSensorReaderDelegate. Load the Delegate Method Catalog when wiring the complete authorization, recording, device-fetch, and sample-fetch lifecycle.

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

PropertyTypeDescription
modelStringUser-defined device name
nameStringFramework-defined device name
systemNameStringOS name (iOS, watchOS)
systemVersionStringOS version
productTypeStringHardware identifier
currentSRDeviceClass property for the running device

Common Mistakes

DON'T: Attempt to use SensorKit without the entitlement

Obtain Apple's study approval, the sensor-specific entitlement values, and a matching manual provisioning profile before constructing production readers.

DON'T: Expect immediate data access

Apply the Data Holding Period; a fetch overlapping the hold is not proof that recording failed.

DON'T: Forget to set the delegate before fetching

Assign the delegate before startRecording() or fetch(_:); results and failures arrive through delegate callbacks.

DON'T: Skip per-sensor Info.plist usage detail

Add the exact Info.plist Configuration usage-detail entry for every requested sensor.

DON'T: Ignore SRError codes

Distinguish at least .invalidEntitlement, .noAuthorization, .dataInaccessible, .fetchRequestInvalid, .promptDeclined, and unknown future codes. Load the Full Delegate Implementation for the complete switch and callback wiring.

Review Checklist

  • Apple-approved research study in place before development
  • com.apple.developer.sensorkit.reader.allow entitlement lists only needed sensors
  • Manual provisioning profile with explicit App ID and SensorKit capability
  • NSSensorKitUsageDescription in Info.plist with clear study purpose
  • NSSensorKitPrivacyPolicyURL in Info.plist with valid privacy policy URL
  • NSSensorKitUsageDetail entries for every requested sensor
  • Required key set appropriately for essential vs. optional sensors
  • Authorization requested before recording, status checked before fetching
  • Delegate assigned before calling startRecording() or fetch(_:)
  • Fetch request time ranges account for 24-hour data holding period
  • SRError codes handled in all failure delegate methods
  • fetchDevices() used to discover available devices before fetching
  • stopRecording() called when data collection is complete
  • sensorReader(_:fetching:didFetchResult:) returns true to continue or false to stop

References