使用SwiftUI构建没有AppDelegate和ViewController的应用程序时,如何实现Linea Pro SDK?
我按照此处所述集成了SDK。如何使用适用于IOS的Linea Pro SDK?我使用了DTDevices.h和libdtdev.a文件,它们可以在这里找到https://github.com/matheuscmpm/lineaswift.
现在,我为尝试使用SDK而编写的类看起来像这样:
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
}
}
在全局作用域中,就在@main
的正上方,我这样初始化这个类:let oLineaScanner = LineaDevice()
。
到目前为止,一切都很好。方法oLineaScanner.getConnectionState()
返回2
,这意味着iOS设备成功连接到扫描仪,当我扫描条形码时,设备会发出嘟嘟声。然而,方法barcodeData
(我认为现在应该由SDK调用(没有被调用。
到目前为止,我能找到的任何文档都假设有一个AppDelegate和一个ViewController,而在我的SwiftUI项目中并不存在。我想这就是问题所在。我对iOS开发相对较新,所以我对如何从这一点开始有点一无所知。
有什么方法可以让它这样工作吗?如果没有,我该如何让它工作?
经过更多的实验,我终于自己弄明白了。毕竟,ViewController是使其工作所必需的。所以我创建了一个新的文件,看起来有点像这样:
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
}
然后我只需将这个LineaView添加到包含其他所有内容的视图中(在我的情况下是一个TabView(,如下所示:
LineaView().hidden()
这对我起到了作用。该应用程序现在按预期对条形码扫描做出响应。
对于其他人面临这个问题的不太可能的事件,给你。希望我不让你受苦。