I have VNDocumentCameraViewController
as a UIViewControllerRepresentable
in a view called ScanView
embedded in a TabView
as the 2nd screen. On dismissing the VNDocumentCameraViewController
(either on cancelling or on saving of the scan), I want the tab view to get back to my first screen. That part works like a charm.
My issue is though that when coming back to my VNDocumentCameraViewController
, I want to reinstantiate that controller and start over–which is what I cannot figure out on how to achieve that.
I am aware that my ContentView
keeping a reference to the ScanView
is why my UIViewControllerRepresentable
is not reinstantiated–how can I do that manually?
Here's the code:
import SwiftUI
@main
struct so_VisionKitInTabsApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State private var tabSelection = 1
var body: some View {
TabView(selection: $tabSelection) {
Text("First View")
.tabItem { Text("First View") }
.tag(1)
ScanView(tabSelection: $tabSelection)
.tabItem { Text("Scan View") }
.tag(2)
}
}
}
import VisionKit
struct DocumentScanningViewAdapter: UIViewControllerRepresentable {
typealias UIViewControllerType = VNDocumentCameraViewController
let onDismiss: () -> ()
func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}
func makeUIViewController(context: Context) -> VNDocumentCameraViewController {
let vc = VNDocumentCameraViewController()
vc.delegate = context.coordinator
return vc
}
func updateUIViewController(_ uiViewController: VNDocumentCameraViewController, context: Context) { }
class Coordinator: NSObject, VNDocumentCameraViewControllerDelegate {
var parent: DocumentScanningViewAdapter
init(parent: DocumentScanningViewAdapter) {
self.parent = parent
}
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
print("Finished successfully…")
parent.onDismiss()
}
func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
print("Cancelled…")
resetCoordinator(for: controller)
parent.onDismiss()
}
func resetCoordinator(for controller: VNDocumentCameraViewController) {
controller.delegate = parent.makeCoordinator()
}
}
}
struct ScanView: View {
@Binding var tabSelection: Int
var body: some View {
DocumentScanningViewAdapter(onDismiss: { tabSelection = 1 })
}
}