0

I have used similar code to the one shown in the following question: How to scan for QR codes on button press?. I can now have the camera preview active all the time and only scan when my button is pressed. My problem is, I have found that if I (accidentally) push the button before being focussed on a QR code, the reader will automatically scan the next QR code it see's. Is there any way I can account for any accidental button presses and only scan the QR code when I have one in view?

Any help would be greatly appreciated!

Community
  • 1
  • 1
evorg88
  • 215
  • 2
  • 12

1 Answers1

0

After a lot of head scratching, I've finally worked out a solution...

I've basically created a button that will appear over my camera preview should a QR code be detected. This button will be hidden by default:

scanButton.isHidden = true

Then used the following code to show the button only if a QR code is found. This is used in conjunction with code to highlight the QR code when found.

if metadataObj.stringValue != nil {
            scanButton.isHidden = false
            view.bringSubview(toFront: scanButton)
            data = metadataObj.stringValue
        }

The action for the scanButton is then:

@IBAction func scanQRButton(_ sender: UIButton) {
    print("Scanned QR: " + (data))
    captureSession?.stopRunning()
}

I will then add a segue function to pass this data to another view controller where I can work with it.

Hope this makes sense and helps someone else avoid all of the head scratching I have done!

evorg88
  • 215
  • 2
  • 12