'How to create and Implement basic login with QRcode?

So for this project I am in I need to implement a login using QR code, but I am new to swift and I don't know how

Right now I am implementing the QR code which scans and gets a string shown, I want that string to be placed into the API ID variable which gets the list of cars in my case from the API call

This is the QRCodeViewController() class, a function which gets the string from the scan:

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()
                alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
                alert.addAction(UIAlertAction(title: "Vazhdo", style: .default, handler: {_ in
                    
                    let idfromQR = object.stringValue
                    let viewController = MonitorimiViewController()
                    viewController.id = idfromQR!
                    
                    let controller = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController")
                    controller?.modalTransitionStyle = .flipHorizontal
                    controller?.modalPresentationStyle = .fullScreen
                    self.present(controller!, animated: true, completion: nil)
                
  
            }))
                
                present(alert, animated: true, completion: nil)
        }
    }
}
}

Now I want that object. StringValue that comes from the QR scanned to be passed into the MonitorimiViewController() which is another class which hold the ID value that needs to get from the object. StringValue but I am struggling to pass that value because it isn't being parsed, I tried all I know but all failed to be parsed.

This is the MonitorimiViewController() class So the object.stringValue must be passed in this var id: String = "object.stringValue"

var id: String = "0E79C6205FD04F8994B13F5255B7FB05"


Solution 1:[1]

You are creating a new viewController, passing the QR code value into it, then you do nothing with the viewController and it falls out of memory.

let idfromQR = object.stringValue
let viewController = MonitorimiViewController()
viewController.id = idfromQR

You don't do anything with viewController after this point. You create a tabBarController from a storyboard, which creates new copies of these classes and open those new copies.

You would need to do something like this instead:

guard let controller = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController") as? UITabBarController,
      let monitorController = controller.viewControllers.first as? MonitorimiViewController else {
    return
}

monitorController.id = idfromQR

self.present(controller, animated: true, completion: nil)

What i'm doing here is:

  • Getting the tabBarController from the storyboard
  • Getting one of the viewControllers it manages (i'm grabbing the first, I don't know what index yours is).
  • Checking that they can all be cast to the correct types.
  • Passing the value into the MonitorimiViewController being used by the tabBar
  • Presenting the tabBar with the updated MonitorimiViewController

Please also make sure to use if let xxx or guard let xxx statements and avoid force unwrapping (e.g. idfromQR!) at all costs. It will crash your app if a problem occurs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simon McLoughlin