I followed the example on another site and configured the DeviceDiscoverySession and then activated the torch. This seems to have resolved the issue.
func toggleTorch() {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualWideCamera, .builtInUltraWideCamera, .builtInWideAngleCamera, .builtInTrueDepthCamera], mediaType: AVMediaType.video, position: cameraPosition) // adapted from [https://www.appsloveworld.com/swift/100/46/avcapturesession-freezes-when-torch-is-turned-on] to fix freezing issue when activating torch
guard let device = deviceDiscoverySession.devices.first 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")
}
}