@@ -14,13 +14,18 @@ import PDFKit
1414
1515@objc ( TiScannerModule)
1616class TiScannerModule : TiModule {
17+
18+ var _scanner : VNDocumentCameraViewController ?
1719
18- lazy var scanner : VNDocumentCameraViewController ? = {
19- let scannerViewController = VNDocumentCameraViewController ( )
20- scannerViewController. delegate = self
20+ func scannerInstance( ) -> VNDocumentCameraViewController {
21+ if let scanner = _scanner {
22+ return scanner
23+ }
24+ _scanner = VNDocumentCameraViewController ( )
25+ _scanner!. delegate = self
2126
22- return scannerViewController
23- } ( )
27+ return _scanner!
28+ }
2429
2530 var currentScan : VNDocumentCameraScan ?
2631
@@ -33,9 +38,8 @@ class TiScannerModule: TiModule {
3338 }
3439
3540 func dismissAndCleanup( ) {
36- scanner? . dismiss ( animated: true , completion: nil )
37- scanner? . delegate = nil
38- scanner = nil
41+ _scanner? . delegate = nil
42+ _scanner = nil
3943 }
4044
4145 // MARK: Public APIs
@@ -47,9 +51,7 @@ class TiScannerModule: TiModule {
4751
4852 @objc ( showScanner: )
4953 func showScanner( unused: [ Any ] ? ) {
50- if let scanner = scanner {
51- TiApp . controller ( ) . present ( scanner, animated: true , completion: nil )
52- }
54+ TiApp . controller ( ) . present ( scannerInstance ( ) , animated: true , completion: nil )
5355 }
5456
5557 @objc ( imageOfPageAtIndex: )
@@ -119,18 +121,24 @@ class TiScannerModule: TiModule {
119121extension TiScannerModule : VNDocumentCameraViewControllerDelegate {
120122 func documentCameraViewControllerDidCancel( _ controller: VNDocumentCameraViewController ) {
121123 fireEvent ( " cancel " )
124+
125+ controller. dismiss ( animated: true , completion: nil )
122126 dismissAndCleanup ( )
123127 }
124128
125129 func documentCameraViewController( _ controller: VNDocumentCameraViewController , didFailWithError error: Error ) {
126130 fireEvent ( " error " , with: [ " error " : error. localizedDescription] )
131+
132+ controller. dismiss ( animated: true , completion: nil )
127133 dismissAndCleanup ( )
128134 }
129135
130136 func documentCameraViewController( _ controller: VNDocumentCameraViewController , didFinishWith scan: VNDocumentCameraScan ) {
131137 currentScan = scan
132138
133139 fireEvent ( " success " , with: [ " count " : scan. pageCount, " title " : scan. title] )
140+
141+ controller. dismiss ( animated: true , completion: nil )
134142 dismissAndCleanup ( )
135143 }
136144}
0 commit comments