DataScannerViewController and torch

Hi,

do you know if it's possible to handle the flashlight inside DataScannerViewController? I tried with AVCaptureDevice.DiscoverySession, but clearly when torchmode is on DataScannerViewController freeze...

Thanks Luca

Hi! Have you found any solution? Looks like DataScannerViewController and Torch can't be used at the same time

Hi ! Any solutions ?

Also looking for a solution, running into the same issue.

I ran into the same issue today when testing on an iPhone. The interesting thing is that when I tested on an iPad the torch came on and off without freezing the DataScannerViewController. They both run the same SwiftUI code with no differences in how the sheet is handled in iOS or iPad OS. I’ll do some more testing tomorrow and see if I can reproduce in a sample app.

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")
        }
    }

This is broken again in iOS17 (up through 17.0.2) as of 9/22/23. Looking for solutions but nothing resolves the issue yet.

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")
                }
    }
DataScannerViewController and torch
 
 
Q