How to turn on the flashlight with iOS Swift
iOS gives us access to the flashlight if there is currently one available on the phone. Technically, its called a torch but whatever lol… Here is a code snippet on how to turn on the torch.
import AVFoundation
func toggleTorch(on: Bool) {
guard let device = AVCaptureDevice.default(for: AVMediaType.video) else { return }
if device.hasTorch {
do {
try device.lockForConfiguration()
if on == true {
device.torchMode = .on
} else {
device.torchMode = .off
}
device.unlockForConfiguration()
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
}