ascscreenbrightnessdetector

Welcome to the documentation for ASCScreenBrightnessDetector!

About ASCScreenBrightnessDetector

ASCScreenBrightnessDetector is a lightweight library that provides an easy way to detect changes in the device screen brightness level. It allows you to monitor and respond to changes in brightness, making it useful in scenarios such as automatic dark mode adjustments, screen brightness-based actions, or any other situation where you need to be aware of changes in brightness.

Using ASCScreenBrightnessDetector

ASCScreenBrightnessDetector is simple to integrate into your iOS project. Here’s how:

Installation

  • Install ASCScreenBrightnessDetector via CocoaPods. Add the following line to your Podfile:
pod 'ASCScreenBrightnessDetector'

Usage

Using ASCScreenBrightnessDetector is straightforward:

import ASCScreenBrightnessDetector
// Initialize the brightness detector
let brightnessDetector = ASCScreenBrightnessDetector()
// Set up a closure to handle changes in brightness level
brightnessDetector.brightnessDidChange = { brightness in
    // Handle the new brightness level here
}

Caution

Remember to retain a strong reference to the ASCScreenBrightnessDetector instance for as long as you need to listen for brightness changes. If the instance is deallocated, it will no longer be able to detect brightness changes.

Additional Notes

Here are some additional things you might want to know:

  • If you wish to customize the sensitivity interval for detecting brightness changes, you can modify the brightnessChangeThreshold property of the ASCScreenBrightnessDetector instance. The default value is set to 0.02.
  • To start detecting brightness changes, call startDetection() on the ASCScreenBrightnessDetector instance. To stop detection, call stopDetection().
  • If you prefer to be notified on the main queue instead of the background queue, use the brightnessDidChangeOnMainQueue property.
  • ASCScreenBrightnessDetector works on iOS 10 and above.

Examples

To help you get started, here are a few examples of how you can utilize ASCScreenBrightnessDetector:

Example 1: Dark Mode Adjustment

You can use ASCScreenBrightnessDetector to automatically switch between light and dark mode based on the device’s brightness level:

import ASCScreenBrightnessDetector
class ViewController: UIViewController {
    
    let brightnessDetector = ASCScreenBrightnessDetector()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        brightnessDetector.brightnessDidChange = { brightness in
            if brightness <= 0.2 {
                // Enable dark mode
                self.overrideUserInterfaceStyle = .dark
            } else {
                // Enable light mode
                self.overrideUserInterfaceStyle = .light
            }
        }
        
        brightnessDetector.startDetection()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        // Stop detecting brightness changes when the view disappears
        brightnessDetector.stopDetection()
    }
    
    // Rest of your code...
}

Conclusion

And that’s it! You’re now equipped with the knowledge to integrate and utilize ASCScreenBrightnessDetector in your projects. Enjoy the convenience of detecting changes in screen brightness and make your apps more responsive to varying lighting environments.