wakel's solution works for me
Getting the device via AVCaptureDevice.userPreferredCamera seems to work on iOS 17.
func setTorchIsOn(_ isOn: Bool) {
var device: AVCaptureDevice?
if #available(iOS 17, *) {
device = AVCaptureDevice.userPreferredCamera
} else {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualWideCamera, .builtInUltraWideCamera, .builtInWideAngleCamera, .builtInTrueDepthCamera], mediaType: AVMediaType.video, position: .back) // adapted from [https://www.appsloveworld.com/swift/100/46/avcapturesession-freezes-when-torch-is-turned-on] to fix freezing issue when activating torch
device = deviceDiscoverySession.devices.first
}
guard let device else { return }
if device.hasTorch && device.isTorchAvailable {
do {
try device.lockForConfiguration()
if torchIsOn {
try device.setTorchModeOn(level: 1.0) // adjust torch intensity here
} else {
device.torchMode = .off
}
device.unlockForConfiguration()
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
}