Posts

Post marked as solved
15 Replies
13k Views
Hi there team.I have this issue, it makes things a little slower in runnning conditionsi have a QR scannerfunc 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 { //this is where the problem starts let alert = UIAlertController(title: "Your code is:", message: object.stringValue, preferredStyle: .alert) if object.stringValue == "1"{ self.performSegue(withIdentifier: "sendToAsistente", sender: nil) } else if object.stringValue == "2"{ self.performSegue(withIdentifier: "sendToExpositor", sender: nil) } else{ print("nada jeje") } alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil)) alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in UIPasteboard.general.string = object.stringValue })) self.present(alert, animated: true, completion: nil) } } } }my Alert is making this issue happendet object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject { if object.type == AVMetadataObject.ObjectType.qr { //this is where the problem starts let alert = UIAlertController(title: "Your code is:", message: object.stringValue, preferredStyle: .alert) if object.stringValue == "1"{ self.performSegue(withIdentifier: "sendToAsistente", sender: nil) } else if object.stringValue == "2"{ self.performSegue(withIdentifier: "sendToExpositor", sender: nil) } else{i have tried toDispatchQueue.main.async { }but it didn't workif object.type == AVMetadataObject.ObjectType.qr { let alert = UIAlertController(title: "Your code is:", message: object.stringValue, preferredStyle: .alert) if object.stringValue == "1"{ DispatchQueue.main.async { self.performSegue(withIdentifier: "sendToAsistente", sender: nil) } } else if object.stringValue == "2"{ DispatchQueue.main.async { self.performSegue(withIdentifier: "sendToExpositor", sender: nil) } }gives me this error :Warning: Attempt to present ContainerViewController: 0x145e4020> on ViewController: 0x146818b0> which is already presenting <UIAlertController: 0x14957a00>that's the error what i'm missing?whole codeimport UIKit import AVFoundation class RegistroViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { // // @IBOutlet weak var square: UIImageView! var video = AVCaptureVideoPreviewLayer() var alertController: UIAlertController? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //Creating session let session = AVCaptureSession() //Define capture devcie let defaultDevice = AVCaptureDevice.default(for: AVMediaType.video) do { let input = try AVCaptureDeviceInput(device: defaultDevice!) 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) self.view.bringSubviewToFront(square) 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: "Your code is:", message: object.stringValue, preferredStyle: .alert) if object.stringValue == "1"{ self.performSegue(withIdentifier: "sendToAsistente", sender: nil) } else if object.stringValue == "2"{ self.performSegue(withIdentifier: "sendToExpositor", sender: nil) } else{ print("nada jeje") } alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil)) alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in UIPasteboard.general.string = object.stringValue })) self.present(alert, animated: true, completion: nil) } } } } //laaaaaaassssstttt }this is the errorWarning: Attempt to present <UIAlertController: 0x183c3a00> on <Conappeme.RegistroViewController: 0x17e7fc70> whose view is not in the window hierarchy!
Posted
by Omar_Boca.
Last updated
.