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
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.
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:
What i’m doing here is:
first, I don’t know what index yours is).
MonitorimiViewController
being used by the tabBarMonitorimiViewController
Please also make sure to use
if let xxx
orguard let xxx
statements and avoid force unwrapping (e.g.idfromQR!
) at all costs. It will crash your app if a problem occursThe problem is in the following lines of code:
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:
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.