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).


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)
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.