iosbegindevel
iosbegindevel

Reputation: 327

What should I do if my dismiss does not work after a QRcode scan?

I am going to scan the QR code in the webView.

QR code scans well and data can be read,

but the problem is, the camera screen won't close after scanning. I'm running a dismiss() function.

webView Load

@IBOutlet weak var indicator: UIImageView!
@IBOutlet var wkWebView: WKWebView!
...
 let config = WKWebViewConfiguration()

        contentController.add(self, name: "native")

        config.userContentController = contentController

        wkWebView = WKWebView(frame: wkWebView.frame, configuration: config)

        wkWebView.uiDelegate = self
        wkWebView.navigationDelegate = self

        view.addSubview(wkWebView)
        view.addSubview(indicator)
let localFilePath = Bundle.main.url(forResource: webUrl, withExtension: "html")
let myRequest = URLRequest(url: localFilePath)
wkWebView.load(myRequest)

QRCode Scan

var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!

   override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if (captureSession?.isRunning == false) {
            captureSession.startRunning()
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if (captureSession?.isRunning == true) {
            captureSession.stopRunning()
        }
    }

    func qrcodeScan(){
        view.backgroundColor = UIColor.black
        captureSession = AVCaptureSession()

        guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
        let videoInput: AVCaptureDeviceInput

        do {
            videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
        } catch {
            return
        }

        if (captureSession.canAddInput(videoInput)) {
            captureSession.addInput(videoInput)
        } else {
            failed()
            return
        }

        let metadataOutput = AVCaptureMetadataOutput()

        if (captureSession.canAddOutput(metadataOutput)) {
            captureSession.addOutput(metadataOutput)

            metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            metadataOutput.metadataObjectTypes = [.qr]
        } else {
            failed()
            return
        }

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.frame = view.layer.bounds
        previewLayer.videoGravity = .resizeAspectFill
        view.layer.addSublayer(previewLayer)

        captureSession.startRunning()
    }

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        captureSession.stopRunning()

        if let metadataObject = metadataObjects.first {
            guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
            guard let stringValue = readableObject.stringValue else { return }
            AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
            found(code: stringValue)
        }

        self.dismiss(animated: true, completion: nil)
    }

    func found(code: String) {
        Log.Info(code)
    }


    func failed() {
        captureSession = nil
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }


    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

As you can see from my code, I'm trying to get the camera window down after reading the QR code.

But the camera screen is still on the frozen screen. No matter how long I wait, I can't see the web view screen. What's the problem?

Upvotes: 0

Views: 277

Answers (1)

Marwen Doukh
Marwen Doukh

Reputation: 2050

The previewLayer is taking all the screen (bounds) and it is hiding the webview, so you should remove the previewLayer from the super layer in order to show the webview.

So the solution is to use this :

previewLayer.removeFromSuperlayer()

instead of

self.dismiss(animated: true, completion: nil)

Upvotes: 1

Related Questions