Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodeScannerView freezes after returning from child page #120

Open
monishkumar7 opened this issue Nov 29, 2023 · 2 comments
Open

CodeScannerView freezes after returning from child page #120

monishkumar7 opened this issue Nov 29, 2023 · 2 comments

Comments

@monishkumar7
Copy link

monishkumar7 commented Nov 29, 2023

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 monishkumar7 changed the title CodeScannerView UI freezes when I navigate away from that screen and come back. CodeScannerView freezes after returning from child page Nov 29, 2023
@epoberezkin
Copy link

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

@monishkumar7
Copy link
Author

monishkumar7 commented Dec 14, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants