Skip to content

🔦 Add torch (flashlight) control #7

@superturboryan

Description

@superturboryan

Overview

Let's add a way to control the torch on the device.

This could go under a new namespace Control.Flashlight 💡

Tactics

Thanks ChatGPT 🤖

Check flashlight availability

import AVFoundation

if let device = AVCaptureDevice.default(for: .video), device.hasTorch {
    print("Torch is available")
} else {
    print("Torch is not available")
}

Turn the Flashlight on or off

func toggleFlashlight(on: Bool) {
    guard let device = AVCaptureDevice.default(for: .video), device.hasTorch else {
        print("Torch is not available")
        return
    }
    
    do {
        try device.lockForConfiguration()
        if on {
            try device.setTorchModeOn(level: AVCaptureDevice.maxAvailableTorchLevel)
        } else {
            device.torchMode = .off
        }
        device.unlockForConfiguration()
    } catch {
        print("Error accessing the torch: \(error.localizedDescription)")
    }
}

Testing

  • Unit tests? 🧪🧪

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions