How to make the Flash work?

I want to use AVCaptureDevice flashMode. But what I have tried all failed.


Reading the net makes it look that should be simple, nevertheless for some reason it crashes.


Here is the code I am using:


if captureDevice.hasFlash && captureDevice.isFlashModeSupported(.On) {
do {try captureDevice.lockForConfiguration()

} catch let error as NSError {

print("captureDevice.lockForConfiguration FAILED")

print(error.code)

}

captureDevice.flashMode = .On

captureDevice.unlockForConfiguration()

captureSession.commitConfiguration()

}


Without this code the app runs fine (with no flash).


With this code:

It compiles OK but when I try to take a photo, the app crashes with the following error:


Error Domain=AVFoundationErrorDomain Code=-11800 "The operation couldn’t be completed." UserInfo={NSUnderlyingError=0x14e176f40 {Error Domain=NSOSStatusErrorDomain Code=-16800 "(null)"}, NSLocalizedFailureReason=Undefined error(-16800), NSLocalizedDescription=The operation couldn’t be completed.}


Since I have never used the flash in an app before, I may well be making an error;

an error in the code above or putting this code in the wrong place.

But I haven’t, at this point, been able to figure out what was the problem.

DId you ever find a solution? I know this was posted more than a year ago, but I am having the same problem. I can't figure out a solution.

How to make the Flash work?
 
 
Q