I\'d like to add flashlight functionality to my app in Swift. How can I go about doing that?
For xcode 9.1, swift 4 (updated to not crash if no torch):
func toggleFlash() {
let device = AVCaptureDevice.default(for: AVMediaType.video)
if (device != nil) {
if (device!.hasTorch) {
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)
}
}
}
}