I'm building a QR code reader into my app, so far I have it open as a sheet and close when a qr/barcode is detected.
The reader part of the app uses UIKit, I have the file QRCodeScan.swift
which is the UIViewControllerRepresentable
, the QR scanner returns the value of the code that it has found into the coordinator in this file.
I can't seem to find any way to get the found code out of the coordinator into the original view.
This is the file QRCodeScan.
struct QRCodeScan: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> ScannerViewController {
let vc = ScannerViewController()
vc.delegate = context.coordinator
return vc
}
func updateUIViewController(_ vc: ScannerViewController, context: Context) {
}
class Coordinator: NSObject, QRCodeScannerDelegate {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
func codeDidFind(_ foundCode: String) {
print(foundCode)
/*this is where the code comes to, need to return it from here */
presentationMode.wrappedValue.dismiss()
}
var parent: QRCodeScan
init(_ parent: QRCodeScan) {
self.parent = parent
}
}
}
This is a cropped down version of the ContentView that calls the qr reader , this is where I need to get the found code back into
struct ContentView: View {
@State var presentQRScanner = false
var body: some View {
NavigationView{
Form{
Section(header: Text("Info")){
Button("Scan Barcode"){
self.presentQRScanner = true
}
.sheet(isPresented: $presentQRScanner){QRCodeScan()}
}
}
.navigationBarTitle(Text("New"), displayMode: .large)
.navigationBarItems(trailing: Button("Save"){
print("Button Pressed")
})
}
}
}
I've hit a total roadblock here, I can't find any resources that allow me to pass the data back from the coordinator, maybe I'm implementing something wrong but I can't seem to adapt any other solutions to fit
Any help is much appreciated.
Thanks