Turn on the light of the iOS device

MLBoy
1 min readOct 6, 2020

--

This is a method using the flashlight programatically.
You can control turning on and off.

It can be controlled from AVCaptureDevice of AVFoundation.

1,Lit

let avCaptureDevice = AVCaptureDevice.default(for: AVMediaType.video)

if avCaptureDevice!.hasTorch, avCaptureDevice!.isTorchAvailable {
do {
try avCaptureDevice!.lockForConfiguration()
try avCaptureDevice!.setTorchModeOn(level: 1.0) // 0.0 ~ 1.0
} catch let error {
print(error)
}
avCaptureDevice!.unlockForConfiguration()
}

t can be lit at the maximum level even with torchMode = .on.
torchMode = .auto says, “The capture device continuously monitors the light level and uses the torch if necessary.”

2,Off

do {
try avCaptureDevice!.lockForConfiguration()
} catch let error {
print(error)
}
avCaptureDevice!.torchMode = .off
avCaptureDevice!.unlockForConfiguration()

🐣

****

Request for work:

rockyshikoku@gmail.com

--

--