skip to Main Content

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"

2

Answers


  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

    Login or Signup to reply.
  2. The problem is in the following lines of code:

    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)
    

    You are assigning the qr code response to a view controller that is never getting presented. So you should instead assign it to one of the tabbar controller’s view controllers.

    Replace the code above with this code to solve your problem:

    guard let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController") as? UITabBarController else {
       // Something wrong with your identifier
       return
    }
    
    (tabBarController.viewControllers[0] as? MonitorimiViewController)?.id = idfromQR!           
    tabBarController.modalTransitionStyle = .flipHorizontal
    tabBarController.modalPresentationStyle = .fullScreen
    self.present(tabBarController, animated: true, completion: nil)
    

    This assumes that MonitorimiViewController is the first index view controller in your tab bar controller, if it is not just change the 0 index to another number.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search