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?

Accepted Reply

I separated the QR code scanning function into another ViewController.


move QRcode ScanScreen


let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let qrcode = storyboard.instantiateViewController(withIdentifier: "QRcodeViewController") as! QRcodeViewController
        qrcode.modalPresentationStyle = .overCurrentContext
        qrcode.modalTransitionStyle = .crossDissolve
        qrcode.delegate = self
self.present(qrcode, animated: false, completion: nil)


QRcodeViewController

import Foundation
import UIKit
import AVFoundation


class QRcodeScannerViewController : UIViewController, AVCaptureMetadataOutputObjectsDelegate {
    var captureSession: AVCaptureSession!
    var previewLayer: AVCaptureVideoPreviewLayer!
    weak var delegate: SendQrcodeDataControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

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

    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 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) {
        print(code)
    }

    func failed() {
        print("can not support camera")
        captureSession = nil
    }


    override var prefersStatusBarHidden: Bool {
        return true 
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
}

Replies

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


Code line 83 should work.

Did you define the class to conform to protocol, such as:

class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {


What I doalso is implement a tap gesture on the view with the IBAction, to allow user to quit and return to the original view:


    @IBAction func scanTapped(_ sender: UITapGestureRecognizer) {
       
        if sender.state == .ended { 
            self.performSegue(withIdentifier: "UnwindFromScanner", sender: self)
        }
    }

The two codes are within the same controller class.



class WebViewController: UIViewController, WKUIDelegate, WKScriptMessageHandler, UIImagePickerControllerDelegate, UINavigationControllerDelegate, SKProductsRequestDelegate, UIScrollViewDelegate, AVCaptureMetadataOutputObjectsDelegate {

I separated the QR code scanning function into another ViewController.


move QRcode ScanScreen


let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let qrcode = storyboard.instantiateViewController(withIdentifier: "QRcodeViewController") as! QRcodeViewController
        qrcode.modalPresentationStyle = .overCurrentContext
        qrcode.modalTransitionStyle = .crossDissolve
        qrcode.delegate = self
self.present(qrcode, animated: false, completion: nil)


QRcodeViewController

import Foundation
import UIKit
import AVFoundation


class QRcodeScannerViewController : UIViewController, AVCaptureMetadataOutputObjectsDelegate {
    var captureSession: AVCaptureSession!
    var previewLayer: AVCaptureVideoPreviewLayer!
    weak var delegate: SendQrcodeDataControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

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

    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 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) {
        print(code)
    }

    func failed() {
        print("can not support camera")
        captureSession = nil
    }


    override var prefersStatusBarHidden: Bool {
        return true 
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
}

Great, that’s the pattern I suggested with ScannerViewController.

Otherwise, you dismiss the WebViewController that called the scanner…