DataScannerViewController freezes when enabling torch using AVCaptureDevice

I am using DataScannerViewController from VisionKit to scan text in my iOS application. The scanner works correctly while running. However, I added a custom button to toggle the device torch (flashlight).

To toggle the torch, I access the camera device using AVCaptureDevice and set torchMode after locking the configuration.

When I turn on the torch while DataScannerViewController is actively scanning, the scanner UI freezes and the camera preview stops updating. The app becomes unresponsive until I leave the screen. This issue appears on all iOS versions.

Code used to toggle the torch:

guard let device = AVCaptureDevice.default(for: .video),
      device.hasTorch else { return }

do {
    try device.lockForConfiguration()
    device.torchMode = device.torchMode == .on ? .off : .on
    device.unlockForConfiguration()
} catch {
    print(error)
}

Questions:

  1. Is it supported to control the torch directly via AVCaptureDevice while DataScannerViewController is running?
  2. Does DataScannerViewController internally lock or manage the camera configuration in a way that can cause this freeze?
  3. Is there a recommended way to enable/disable the torch when using DataScannerViewController?

Environment:

  • VisionKit DataScannerViewController
  • iPhone device (not simulator)
  • iOS versions where freeze occurs: All

Any clarification about whether this behavior is expected or a limitation of VisionKit would be appreciated.

DataScannerViewController freezes when enabling torch using AVCaptureDevice
 
 
Q