0

How do I implement the Linea Pro SDK when building an app with SwiftUI that has no AppDelegate and no ViewController?

I integrated the SDK as described here How do I use the Linea-Pro SDK for IOS? and I used the DTDevices.h and libdtdev.a files that can be found here https://github.com/matheuscmpm/lineaswift.

Now the class I wrote to try and work with the SDK looks something like this:

import Foundation

class LineaDevice: DTDeviceDelegate {

private let oScanner: DTDevices

init() {
    self.oScanner = DTDevices()
    self.oScanner.delegate = self
    self.oScanner.connect()
}

func barcodeData(barcode: String!, type: Int32) {
    print("Barcode: \(barcode!)")
}

public func getConnectionState() -> Int32 {
    return self.oScanner.connstate
}

}

In the global scope, right above @main, I initialize this class like so: let oLineaScanner = LineaDevice().

So far, so good. The method oLineaScanner.getConnectionState() returns 2, which means the iOS device successfully connects to the scanner and when I scan a barcode, the device beeps. However, the method barcodeData - which I assumed should now be getting called by the SDK - does not get called.

Any documentation I could find so far assumes that there is an AppDelegate and a ViewController, which doesn't exist in my SwiftUI project. I assume that that's the issue here. I am relatively new to developing for iOS so I am kinda clueless on how to proceed from this point on.

Is there any way I can make it work like this and if not then how do I make it work?

MFischer
  • 1
  • 3

1 Answers1

0

After some more experimenting, I finally figured it out myself. A ViewController was necessary after all to get it to work. So I created a new File that looks little something like this:

struct LineaView: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -> LineaViewController { return LineaViewController() }
    func updateUIViewController(_ uiViewController: LineaViewController, context: Context) { }
    func makeCoordinator() -> LineaView.Coordinator { return Coordinator(self) }

}

extension LineaView {

    class Coordinator {
    
        var parent: LineaView
    
        init(_ parent: LineaView) {
            self.parent = parent
        }
    
    }

}

class LineaViewController: UIViewController, DTDeviceDelegate {
    // function barcodeData() is implemented here and other related logic for that matter. Don't forget to implement viewDidLoad() where you connect to the device
}

Then I just add this LineaView to the View that contains everything else (a TabView in my case) like this:

LineaView().hidden()

That did the trick for me. The App now responds to a barcode scan as intended.

For the unlikely event that anybody else faces this issue, here you go. Hope I spared you the suffering.

MFischer
  • 1
  • 3