Skip to content

A Swift package that uses IOKit private APIs and exposes system sensor data including thermal, voltage, current, memory, etc.

License

Notifications You must be signed in to change notification settings

getmirai/swift-sensors

Repository files navigation

SwiftSensors

Platform Compatibility Swift Version Listen to our Podcast View our Deck Contact Us

A Swift package for accessing real device sensor readings, including thermal, voltage, current, memory, CPU, and disk statistics.

Features

  • Access thermal, voltage, current sensors readings
  • Memory usage statistics
  • CPU usage statistics
  • Disk space information
  • System information
  • Battery information
  • Formatted output for human-readable display

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/getmirai/swift-sensors.git", from: "0.0.1")
]

Usage

import SwiftSensors

// Get an instance of the sensor manager
let sensors = SwiftSensors.shared

// Get thermal sensor readings
let thermalReadings = await sensors.getThermalSensorReadings()
for reading in thermalReadings {
    print("\(reading.name): \(SensorFormatter.shared.formatTemperature(reading.temperature))")
}

// Get voltage sensor readings
let voltageReadings = await sensors.getVoltageSensorReadings()
for reading in voltageReadings {
    print("\(reading.name): \(reading.voltage)V")
}

// Get current sensor readings
let currentReadings = await sensors.getCurrentSensorReadings()
for reading in currentReadings {
    print("\(reading.name): \(reading.current)A")
}

// Get memory statistics
let memoryStats = await sensors.getMemoryStats()
print("Memory Usage: \(await sensors.getFormattedMemoryUsage())")

// Get CPU statistics
let cpuStats = await sensors.getCPUStats()
print("CPU Usage: \(await sensors.getFormattedCPUUsage())")

// Get disk statistics
let diskStats = await sensors.getDiskStats()
print("Disk Usage: \(await sensors.getFormattedDiskUsage())")

// Get thermal state
let thermalState = await sensors.getThermalState()
print("Thermal State: \(thermalState.rawValue)")

// Get system uptime
print("System Uptime: \(await sensors.getFormattedUptime())")

Note on Private APIs

This package uses private APIs to access internal device sensors including thermal, voltage, and current readings. While this works well for development and diagnostic purposes, be aware that using private APIs may not be acceptable for App Store submissions.

License

This project is available under the MIT license. See the LICENSE file for more info.