Calling Barcodescanner from another class, cant retrieve the data?

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.

Code Block import AVFoundation
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)
}
}


Code Block
Partynallen().readerVC.delegate = Partynallen().self // DONT KNOW IF THIS IS CORRECT?

No, it is not.

And
Code Block
Partynallen().readerVC.modalPresentationStyle = .formSheet
present(Partynallen().readerVC, animated: true)

isn't either.

Each time you call Partynallen(), you create a new instance which disappears when you end the func.

Is Partynallen associated to a VC in IB ?

To keep an instance, you have to create it:

Code Block
class NewViewController: UIViewController {
var partynallen: Partynallen?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
partynallen = Partynallen()
// readerVC.delegate is already initiated in Partynallen ; what do you want to do here ?
// 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)
partynallen.readerVC.modalPresentationStyle = .formSheet
present(partynallen.readerVC, animated: true)
}
}

Hi!

Thanks for taking your time!

Before i applied your code, the barcode had a cancel button which didnt respond when pushing, after applying your code i can push it but the image freezes when i do, same thing happens when i scan a barcode.

I am very new to coding so sorry if i am not answering this questions properly, but i think no, Partyhallen is not associated to a viewcontroller, only "NewViewController" is. The partyhallen class is only made so i can seperate code (i want the barcode code to be on a seperate class so i can read my code easier). I basically want all my functions/methods to be in the "partyhallen" class, and just call them from "NewViewController".

Thanks again!
Could you show how you define QRCodeReaderViewController ?
Hi!

I think you mean the QRCodeReaderViewController.swift file? I am not at my mac computer at the moment, but ill update it when i get home.

All the files can also be found at cocoapods.org/pods/QRCodeReader.swift , i havent fiddled with anything the pod came with.

Thanks again for taking your time in helping me, very appreciated.
In addition to the code, could you tell:
  • do you ask for authorisation to use the camera ?

  • did you set the info.plist entry to ask for permission ?

Before i applied your code, the barcode had a cancel button which didnt respond when pushing, after applying your code i can push it but the image freezes when i do, same thing happens when i scan a barcode.

  • where does it freeze exactly ? Do you see the camera image ?

Hi Claude!

Yes, the permission works.

Actually if i shove the code into the same viewcontroller class (lets say we delete the class NewViewController and put the button in Partynallen instead) everything works, it scans and returns the barcode etc, its just now that ive created a separate viewcontroller class and seperated the button and the (qrcode) that the barcode freezes.

So at the moment i can open the barcode and have everything work good (i can see the image etc), but when it actually scans a barcode it just freezes, same when i press the cancel button from within the barcode.
Hi Claude!

I am going to try to rephrase my problem and also link the QRCodeReaderViewController.swift file below.

Basically i had a button on my app that opens upp a barcode scanner, I could then scan an item and it prints the barcode etc.
My Viewcontroller was getting very full of codes, and i just wanted to create another class to seperate and make my code easier to read. So i put all of the barcodescanner code in the class "Partnyallen" and kept my button in class "NewViewController". I can open the barcode, but as soon as i scan an item or even if i press the "cancel" button inside, it just freezes. I want it to act normal and print out my barcodes etc, basically return the results.

Here comes the QRCodeReaderViewController definition/code.

Code Block
import UIKit
import AVFoundation
/// Convenient controller to display a view to scan/read 1D or 2D bar codes like the QRCodes. It is based on the `AVFoundation` framework from Apple. It aims to replace ZXing or ZBar for iOS 7 and over.
public class QRCodeReaderViewController: UIViewController {
private let builder: QRCodeReaderViewControllerBuilder
/// The code reader object used to scan the bar code.
public var codeReader: QRCodeReader {
return builder.reader
}
// MARK: - Managing the Callback Responders
/// The receiver's delegate that will be called when a result is found.
public weak var delegate: QRCodeReaderViewControllerDelegate?
/// The completion blocak that will be called when a result is found.
public var completionBlock: ((QRCodeReaderResult?) -> Void)?
deinit {
codeReader.stopScanning()
NotificationCenter.default.removeObserver(self)
}
// MARK: - Creating the View Controller
/**
Initializes a view controller using a builder.
- parameter builder: A QRCodeViewController builder object.
*/
required public init(builder: QRCodeReaderViewControllerBuilder) {
self.builder = builder
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .black
codeReader.didFindCode = { [weak self] resultAsObject in
if let weakSelf = self {
if let qrv = builder.readerView.displayable as? QRCodeReaderView {
qrv.addGreenBorder()
}
weakSelf.completionBlock?(resultAsObject)
weakSelf.delegate?.reader(weakSelf, didScanResult: resultAsObject)
}
}
codeReader.didFailDecoding = {
if let qrv = builder.readerView.displayable as? QRCodeReaderView {
qrv.addRedBorder()
}
}
setupUIComponentsWithCancelButtonTitle(builder.cancelButtonTitle)
}
required public init?(coder aDecoder: NSCoder) {
self.builder = QRCodeReaderViewControllerBuilder()
super.init(coder: aDecoder)
}
// MARK: - Responding to View Events
override public func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if builder.startScanningAtLoad {
builder.readerView.displayable.setNeedsUpdateOrientation()
startScanning()
}
}
override public func viewWillDisappear(_ animated: Bool) {
stopScanning()
super.viewWillDisappear(animated)
}
override public func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
codeReader.previewLayer.frame = view.bounds
}
public override var preferredStatusBarStyle: UIStatusBarStyle {
return builder.preferredStatusBarStyle ?? super.preferredStatusBarStyle
}
// MARK: - Initializing the AV Components
private func setupUIComponentsWithCancelButtonTitle(_ cancelButtonTitle: String) {
view.addSubview(builder.readerView.view)
builder.readerView.view.translatesAutoresizingMaskIntoConstraints = false
builder.readerView.setupComponents(with: builder)
// Setup action methods
builder.readerView.displayable.switchCameraButton?.addTarget(self, action: #selector(switchCameraAction), for: .touchUpInside)
builder.readerView.displayable.toggleTorchButton?.addTarget(self, action: #selector(toggleTorchAction), for: .touchUpInside)
builder.readerView.displayable.cancelButton?.setTitle(cancelButtonTitle, for: .normal)
builder.readerView.displayable.cancelButton?.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
// Setup constraints
for attribute in [.left, .top, .right] as [NSLayoutConstraint.Attribute] {
NSLayoutConstraint(item: builder.readerView.view, attribute: attribute, relatedBy: .equal, toItem: view, attribute: attribute, multiplier: 1, constant: 0).isActive = true
}
if #available(iOS 11.0, *) {
view.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: builder.readerView.view.bottomAnchor).isActive = true
}
else {
NSLayoutConstraint(item: builder.readerView.view, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
}
}
// MARK: - Controlling the Reader
/// Starts scanning the codes.
public func startScanning() {
codeReader.startScanning()
}
/// Stops scanning the codes.
public func stopScanning() {
codeReader.stopScanning()
}
// MARK: - Catching Button Events
@objc func cancelAction(_ button: UIButton) {
codeReader.stopScanning()
if let _completionBlock = completionBlock {
_completionBlock(nil)
}
delegate?.readerDidCancel(self)
}
@objc func switchCameraAction(_ button: SwitchCameraButton) {
if let newDevice = codeReader.switchDeviceInput() {
delegate?.reader(self, didSwitchCamera: newDevice)
}
}
@objc func toggleTorchAction(_ button: ToggleTorchButton) {
codeReader.toggleTorch()
}
}

Calling Barcodescanner from another class, cant retrieve the data?
 
 
Q