Wesley de Groot's Blog
Thermal States on iOS

Back

You can read the thermal states on iOS documentation to understand how to manage the thermal state of your iOS application. This is important for optimizing performance and battery life.

What is Thermal State?

Thermal states on iOS refer to the different levels of thermal conditions that an iOS device can experience. These states are used by the system to manage performance and power consumption based on the device's temperature. The thermal state can affect how your application behaves, especially in terms of CPU and GPU usage.

Reasons to know the Thermal State

For example if you want to compress a video, you might want to know the thermal state of the device to avoid overheating. If the device is in a high thermal state, you might want to reduce the quality of the video compression or pause it to prevent the device from overheating.

How to read the Thermal State

You can read the thermal state of an iOS device using the ProcessInfo class. The thermalState property of ProcessInfo provides the current thermal state of the device. You can use this information to adjust your application's behavior based on the thermal conditions.

import Foundation

let thermalState = ProcessInfo.processInfo.thermalState

switch thermalState {
case .nominal:
    print("Device is in a nominal thermal state.")
case .fair:
    print("Device is in a fair thermal state.")
case .serious:
    print("Device is in a serious thermal state.")
case .critical:
    print("Device is in a critical thermal state.")
case .unknown:
    print("Device thermal state is unknown.")
default:
    print("Device thermal state is not recognized.")
}

Example for video compression

import Foundation
var videoCompressionInProgress = true

func shouldContinueVideoCompression() -> Bool {
    let thermalState = ProcessInfo.processInfo.thermalState

    switch thermalState {
    case .nominal, .fair:
        // Continue compression at normal quality
        return true
    case .serious, .critical:
        // Reduce quality or pause compression to prevent overheating
        return false
    case .unknown:
        // Handle unknown state cautiously
        return false
}

func compressFrame() {
    // Your video compression logic here
    print("Compressing video frame...")
}

while (shouldContinueVideoCompression() && videoCompressionInProgress) {
    compressFrame() // Your video compression logic here
}

Conclusion

Knowing the thermal state of the device is curcial if you want to execute work which can cause the device to overheat. By using the ProcessInfo class, you can read the thermal state and adjust your application's behavior accordingly. This can help you optimize performance and battery life, ensuring a better user experience.

Read more

Share


Share Bluesky Mastodon Twitter LinkedIn Facebook
x-twitter mastodon github linkedin discord threads instagram whatsapp bluesky square-rss sitemap