The torch button in Lensir worked. The light came on. Then the camera froze on whatever it was pointing at, and it stayed frozen until I turned the torch off again. A scanner you can't use in the dark is half a scanner.
Lensir is the offline QR scanner I've been building. It reads a code and checks the link before you open it, looking for lookalike letters, shorteners, plain http and raw IP addresses. This post is about one bug in it, the torch that froze the camera, and the probe that finally told me why.
The obvious torch code
The scanner is VisionKit's DataScannerViewController. It runs its own capture session and doesn't let you touch it. So to switch the torch, I did what most examples do: ask AVFoundation for the default video camera, lock it, set the torch mode.
let device = AVCaptureDevice.default(for: .video)
try device.lockForConfiguration()
try device.setTorchModeOn(level: AVCaptureDevice.maxAvailableTorchLevel)
device.unlockForConfiguration()It compiles, the light comes on, and the picture stops.
Guessing wasn't going to work
I had two theories. One was glare, where the torch washes out a glossy code so the scanner can't read it. The other was that I was fighting VisionKit for the camera. They need different fixes, so I asked one question first: does the picture itself freeze, or does it keep moving and never find the code? It froze. That pointed at the camera, not the light.
Then I wanted proof rather than a hunch. I added temporary logging for every AVCaptureSession notification and launched the app on the phone with devicectl's console flag, so its output streamed to my Mac. Turning the torch on printed this, trimmed:
TORCHDBG set true device=AVCaptureDeviceTypeBuiltInWideAngleCamera
TORCHDBG note AVCaptureSessionWasInterruptedNotification [AVCaptureSessionInterruptionReasonKey: 3]
TORCHDBG note AVCaptureSessionDidStopRunningNotificationReason 3 is videoDeviceInUseByAnotherClient. iOS thought another app had taken the camera.
One camera, several devices
On an iPhone 12 Pro, the back camera is several AVCaptureDevice objects at once: the wide, ultra wide and telephoto lenses on their own, plus virtual devices that combine them. VisionKit runs one of those virtual devices. AVCaptureDevice.default(for: .video) hands you the plain wide camera.
So when I locked the wide camera to change its torch, I was configuring a device my session wasn't running. iOS treats that as a second client grabbing the hardware and suspends the session until the lock is gone. VisionKit doesn't tell you which device it chose, so I wrote a probe. It switched the torch on through each back camera in turn, waited a second and a half, and checked for an interruption:
- Triple camera: no interruption
- Dual wide, dual, wide, ultra wide, telephoto: interrupted, every one
One out of six. The scanner was running the triple camera.
The fix
The torch now goes on the device the scanner is actually using. When VisionKit is scanning, that's the most capable virtual camera on the back: triple, then dual wide, then dual, then wide. Lensir also has a plain AVFoundation fallback, whose session runs the default camera, so in that mode the torch uses the default camera too. The preference order lives in a small pure type with tests, because it's exactly the kind of rule a future refactor would quietly undo.
I don't own every iPhone, and VisionKit could pick differently on another model. So there's a safety net. If the session is ever interrupted with videoDeviceInUseByAnotherClient while the torch is on, Lensir turns the torch off and says "The torch isn't available with this camera". The picture comes back instead of sitting frozen.
All 271 unit tests and 20 UI tests pass. On my phone, the torch comes on and the code still scans.
Where it leaves me
The whole bug was one line asking AVFoundation for the wrong camera. I only found it because I stopped guessing and logged what the session was doing. If a camera feature misbehaves on you, start there: watch for interruptions before you change any code. Lensir is coming soon on GitHub. If you're building something with a camera in it and it's fighting you, that's the kind of problem I like working on.





