Every iOS device has the ability to be used as a torch. Developers have access to use this torch through AVCaptureDevice. You can copy this function into your app and using the setTorch boolean parameter, you can switch it on and off. You can also adjust the level of intensity on the torch by adjusting the setTorchModeOn parameter anywhere between 0 and 1 (maximum).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setTorch(on: Bool) { | |
guard let device = AVCaptureDevice.default(for: AVMediaType.video), | |
device.hasTorch else { | |
return | |
} | |
do { | |
try device.lockForConfiguration() | |
if on { | |
do { | |
try device.setTorchModeOn(level: 1.0) | |
} catch { | |
print(error) | |
} | |
} else { | |
device.torchMode = AVCaptureDevice.TorchMode.off | |
} | |
device.unlockForConfiguration() | |
} catch { | |
print(error) | |
} | |
} |