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
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
Turn the Flashlight on or off
Testing