How to turn flashlight ON and OFF in swift? How to turn flashlight ON and OFF in swift? ios ios

How to turn flashlight ON and OFF in swift?


Update #1: (torchActive isn't returning the expected value; perhaps because it's been modified)

Update #2: For Swift 2.0

To toggle the flash from on to off (not just "on" as in mad pig's answer), you can use the following method:

func toggleFlash() {    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)    if (device.hasTorch) {        do {            try device.lockForConfiguration()            if (device.torchMode == AVCaptureTorchMode.On) {                device.torchMode = AVCaptureTorchMode.Off            } else {                do {                    try device.setTorchModeOnWithLevel(1.0)                } catch {                    print(error)                }            }            device.unlockForConfiguration()        } catch {            print(error)        }    }}

I used nested do-catch blocks to implement Awesomeness's suggestion from the comments. This way, even if try device.setTorchModeOnWithLevel(1.0) fails, the device is properly unlocked for configuration.

Update #3: For Swift 4:

(I edited the code a bit to my personal taste)

func toggleFlash() {    guard let device = AVCaptureDevice.default(for: AVMediaType.video) else { return }    guard device.hasTorch else { return }    do {        try device.lockForConfiguration()        if (device.torchMode == AVCaptureDevice.TorchMode.on) {            device.torchMode = AVCaptureDevice.TorchMode.off        } else {            do {                try device.setTorchModeOn(level: 1.0)            } catch {                print(error)            }        }        device.unlockForConfiguration()    } catch {        print(error)    }}

Original answer:

To toggle the flash from on to off (not just "on" as in mad pig's answer), you can use the following method:

func toggleFlash() {    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)    if (device.hasTorch) {        device.lockForConfiguration(nil)        let torchOn = !device.torchActive        device.setTorchModeOnWithLevel(1.0, error: nil)        device.torchMode = torchOn ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off        device.unlockForConfiguration()    }}


Updated Swift 4 Answer:

func toggleTorch(on: Bool) {    guard         let device = AVCaptureDevice.default(for: AVMediaType.video),        device.hasTorch    else { return }    do {        try device.lockForConfiguration()        device.torchMode = on ? .on : .off                            device.unlockForConfiguration()    } catch {        print("Torch could not be used")    }}

Then to actually turn it on or off, call the function and pass in a true or false boolean.

toggleTorch(on: true) of toggleTorch(on: false)

I got this answer from Hacking with Swift, however their example had an error in it.

They used AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) but this produces an error saying defaultDevice doesn't exist. So I changed it to AVCaptureDevice.default(for: AVMediaType.video)


I've updated @Lyndsey Scott's great answer for Swift 2.0

let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)    if (device.hasTorch) {        do {            try device.lockForConfiguration()            if (device.torchMode == AVCaptureTorchMode.On) {                device.torchMode = AVCaptureTorchMode.Off            } else {                try device.setTorchModeOnWithLevel(1.0)            }            device.unlockForConfiguration()        } catch {            print(error)        }    }