The Torch That Froze My iPhone Camera

In Lensir, my offline QR scanner, turning on the torch froze the camera until I turned it off. The cause was switching the torch through a different camera device than the one VisionKit was running. Here's how I found it and the fix.

Published: November 8, 2026

Last updated: November 8, 2026

Lensir's scanner on an iPhone with the torch on, its camera stuck on a blurred desk.
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 AVCaptureSessionDidStopRunningNotification

Reason 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.

Frequently asked questions

Why does turning on the torch freeze the camera in my iOS app?

Most likely you're locking a different AVCaptureDevice than the one your capture session is running. iOS treats that as another client taking the camera and interrupts your session with videoDeviceInUseByAnotherClient until you release it. Switch the torch on the exact device the session uses.

Which camera does VisionKit's DataScannerViewController use?

It doesn't expose its device. On my iPhone 12 Pro it was the virtual triple camera. AVCaptureDevice.default(for: .video) returns the plain wide camera instead, which is why the obvious torch code froze it.

How did you find which camera it was?

With a probe build. It switched the torch on through each back camera in turn, listened for AVCaptureSession interruption notifications, and printed the result through devicectl's console. Only the triple camera left the scanner running.

Is Lensir available?

The code is open source on GitHub. It's an offline iOS QR scanner that checks a link for lookalike letters, shorteners, plain http and raw IP addresses before you open it.

Can you help with a camera or scanning feature in my app?

Yes. I build iOS and web apps where the feel matters, including camera and scanning flows. Tell me what you're building and where it's getting stuck.

More Articles

Let's Work
Together

Hand Reaching

Have a project in mind? We'd love to hear about it. Let's create something great together!

PRODUCT DEVELOPERUI ENGINEERMVP DEVELOPMENTFRONTEND DEVELOPMENT