UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Calling Barcodescanner from another class, cant retrieve the data?

Forums > Swift

Hi!

I'm trying to open the barcode scanner from class "Partynallen" with a button placed in "NewViewController". I can get the barcode to open and scan stuff, but the image freezes and no result is being passed. What could i possibly be doing wrong?

Appreciate any help provided, thanks in advance.

import QRCodeReader
import Alamofire
class Partynallen: UIViewController, QRCodeReaderViewControllerDelegate {
    lazy var readerVC: QRCodeReaderViewController = {
    let builder = QRCodeReaderViewControllerBuilder {
    //change object to scan and the initial position of the camera
        $0.reader = QRCodeReader(metadataObjectTypes: [.qr, .ean13], captureDevicePosition: .back)
    // Configure the view controller (optional)
    $0.showTorchButton = true
    $0.showSwitchCameraButton = false
    $0.showCancelButton = true
    $0.showOverlayView = true //shows the square area of the QRCode Scanner
        $0.rectOfInterest = CGRect(x: 0, y: 0, width: 1, height: 1)
    }
    return QRCodeReaderViewController(builder: builder)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        readerVC.delegate = self
    }

    func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
    //code to be added
        reader.stopScanning()
        //print(result)
        //print(result.value)
        //barcode = result.value
       //Apifetch(code: "URL")
        dismiss(animated: true, completion: nil)
    }
    func readerDidCancel(_ reader: QRCodeReaderViewController) {
    //code to be added
        reader.stopScanning()
        dismiss(animated: true, completion: nil)
    }

}
class NewViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        Partynallen().readerVC.delegate = Partynallen().self // DONT KNOW IF THIS IS CORRECT?
    }
        @IBAction func scan(_ sender: UIButton) {
            Partynallen().readerVC.modalPresentationStyle = .formSheet
            present(Partynallen().readerVC, animated: true)
    }

}

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.