Home > Mobile >  How to parse a string that comes from scanning a QR code to a variable in a different class
How to parse a string that comes from scanning a QR code to a variable in a different class

Time:01-19

I have the code to scan QR code from my app but i want the string that is obtained from the QR code to be passed into a variable of my choice.

The app's functionality is to login with QR code and that string is the ID to show the users specific data

Here is the code of my QR code scanning and the part of the addAction is the one that shows what you got when you scanned the QR code and I want that string that shows in the popup alert to be parsed to a global variable so the user will login with the ID obtained from the QR.

I searched a lott about this specific topic on loging in with QR code or how to parse data from QR code to variables but nothing came up and i thought why not publish here and help someone else who will struggle in the future :)

class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    var video = AVCaptureVideoPreviewLayer()

    override func viewDidLoad() {
        super.viewDidLoad()
    
        //Creating session
        let session = AVCaptureSession()
    
        //Define capture device
        let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
    
        do {
            let input = try AVCaptureDeviceInput(device: captureDevice!)
            session.addInput(input)
        } catch {
            print("ERROR")
        }
    
        let output = AVCaptureMetadataOutput()
        session.addOutput(output)
    
        output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
    
        output.metadataObjectTypes = [AVMetadataObject.ObjectType.qr]
    
        video = AVCaptureVideoPreviewLayer(session: session)
        video.frame = view.layer.bounds
        view.layer.addSublayer(video)
    
        session.startRunning()
    }

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        if metadataObjects != nil && metadataObjects.count != 0 {
            if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
                if object.type == AVMetadataObject.ObjectType.qr {
                    let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
                    alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in
                        UIPasteboard.general.string = object.stringValue

                    }))
                
                    present(alert, animated: true, completion: nil)
                }
            }
        }
    }
}

CodePudding user response:

You have got that string in the function below, you just have to set that id where you want, like this

 func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
    if metadataObjects != nil && metadataObjects.count != 0 {
        if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
            if object.type == AVMetadataObject.ObjectType.qr {

            // You have got the string 
            let id = object.stringValue

            // you can set the id where ever you want but avoid global value
              
            }
        }
    }
}
  •  Tags:  
  • Related