CodeScanner icon indicating copy to clipboard operation
CodeScanner copied to clipboard

CodeScannerView freezes after returning from child page

Open monishkumar7 opened this issue 2 years ago • 2 comments

Description: I have encountered an issue with the CodeScannerView where it freezes after returning from a child page.

Steps to Reproduce

  1. Navigate to the page containing the CodeScannerView.
  2. Navigate to a child page.
  3. Return to the page containing the CodeScannerView.
  4. Observe that the CodeScannerView becomes unresponsive and frozen.

Expected Behavior

After returning from the child page, the CodeScannerView should continue to function as expected.

Actual Behavior

The CodeScannerView freezes. I don't see the green light on the Dynamic Island too. I think the camera connection is cut off.

monishkumar7 avatar Nov 29 '23 19:11 monishkumar7

That is indeed the case, and the only workaround I found was to force swiftUI to recreate the view this way:

// The hack below prevents camera freezing when exiting child view.
// Using showQRCodeScanner inside qrCodeScannerView or passing it as parameter still results in freezing.
if showQRCodeScanner {
    qrCodeScannerView()
} else {
    qrCodeScannerView(showScanner: false)
}

private func qrCodeScannerView(showScanner: Bool = true) -> some View {
    ZStack {
        if showScanner {
            CodeScannerView(codeTypes: [.qr], completion: processQRCode)
        }
    }
}

epoberezkin avatar Dec 02 '23 11:12 epoberezkin

That is indeed the case, and the only workaround I found was to force swiftUI to recreate the view this way:

// The hack below prevents camera freezing when exiting child view.
// Using showQRCodeScanner inside qrCodeScannerView or passing it as parameter still results in freezing.
if showQRCodeScanner {
    qrCodeScannerView()
} else {
    qrCodeScannerView(showScanner: false)
}

private func qrCodeScannerView(showScanner: Bool = true) -> some View {
    ZStack {
        if showScanner {
            CodeScannerView(codeTypes: [.qr], completion: processQRCode)
        }
    }
}

Thank you.

I used the QRCodeScannerView and then used Sheet to keep the background QRCodeScannerView active. That worked.

monishkumar7 avatar Dec 14 '23 13:12 monishkumar7