Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS QR code scanner triggering actions multiple times

I am using the QR code reader code from AppCoda (http://www.appcoda.com/qr-code-reader-swift/#comments) and converted it to Swift 3. The base code works perfectly fine.

However, what I want to achieve is to retrieve a String from the QR reader, store it in a variable, and pass the variable to a following view controller. So I added a bit of code to it to get the following:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

    if metadataObjects == nil || metadataObjects.count == 0 {
        qrCodeFrameView?.frame = CGRect.zero
        messageLabel.text = "No barcode/QR code is detected"
        return
    }


    let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

    if supportedBarCodes.contains(metadataObj.type) {
        let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
        qrCodeFrameView?.frame = barCodeObject!.bounds

        if metadataObj.stringValue != nil {
            messageLabel.text = metadataObj.stringValue

        }
        //
        //
        // CODE I ADDED STARTS BELOW
        //
        performSegue(withIdentifier: "showMenu", sender: self)
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showMenu"{

        let tabVc = segue.destination as! UITabBarController
        let navVc = tabVc.viewControllers?.first as! UINavigationController
        let menuVc = navVc.viewControllers.first as! MenuViewController

        menuVc.qrScan = self.messageLabel.text
    }

It actually achieves what I wanted it to do, but it somehow triggers the "performSegue" multiple times. The transition animation of the first segue goes halfway, and then the second one happens.

The segue "showMenu" is a manual segue connected from the view controller of the QR Reader view to a tab bar controller that houses a navigation controller and Menu view controller.

Things I've tried:

  1. Print the passed on variable in the viewdidload function of Menu view controller. The string gets printed out twice.

  2. Added a hidden button on the QR reader view and change the segue connection of "showMenu" from the view controller to the button. Removed perform segue from code. While holding the camera in place to scan qr codes, pressing the button performs the intended function and the segue is only triggered once.

  3. With the same setting as (2), I programmatically trigger the button by using a touch up inside event when a qr code is scanned. The segue gets triggered twice.

  4. Added breakpoints for performSegue and the if clause right above it. After I scan something, and press the "continue program execution" button, the program loops between the two breakpoints on and on.

If anyone can enlighten me a bit over here, I would be very grateful. Thanks a bunch for the help in advance.

like image 769
JustTro11 Avatar asked Mar 10 '23 17:03

JustTro11


1 Answers

The issue was fixed by adding the following line of code after the segue was performed:

self.captureSession?.stopRunning()

However, the question of why doesn't unwind segue have a similar issue still remains.

like image 167
JustTro11 Avatar answered Mar 24 '23 15:03

JustTro11