This projects demonstrates a problem in SwiftUI when embedding DataScannerViewController
in
UIViewControllerRepresentable
. It has been created based on StackOverflow questions
and its associated code.
-
DataScannerViewController
is wrapped in aUIViewControllerRepresentable
conformingDataScannerView
(in file DataScannerView.swift). It will detect barcode/text elements (based on some input parameters). -
ViewInsideScanView
is embedding theDataScannerView
if access to the camera has been granted, otherwise a text message is shown.
TheheaderView
(presented as a sheet) allows changing the type of scan. -
ScanView
is integratingViewInsideScanView
in its layout. -
ContentView
is combining a dummy "Home" view and theScanView
in aTabView
.
The DataScannerViewController
automatically stopps scanning and updating the camera preview, when the view disappears
by switching the currently selected tab. When switching the tab back to the scanner view, the scanning does not restart,
even though method startScanning()
of DataScannerViewController
is called.
Recreating the DataScannerView
instance is currently the only way to make it work again.
This is done by incrementing the appearCount
of the current ScannerDataViewModel
instance. This causes the
dataScannerViewId
to change, making SwiftUI think, that the DataScannerView
has to be re-instantiated.