@capgo/capacitor-document-scanner 8.4.13 → 8.4.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +1,8 @@
1
- // swiftlint:disable file_length type_body_length identifier_name type_name function_body_length
1
+ // swiftlint:disable file_length type_name function_body_length
2
2
  import CoreImage
3
- import ObjectiveC.runtime
4
3
  import UIKit
5
4
  import VisionKit
6
5
 
7
- private final class WeakDocumentCameraController {
8
- weak var value: VNDocumentCameraViewController?
9
- }
10
-
11
- private let activeDocumentCameraController = WeakDocumentCameraController()
12
- private var documentScanLimit: Int?
13
- private var documentScanShouldPresentPreviewAfterCapture = false
14
- private var documentScanLastPreviewedAcceptedCount = 0
15
- private var documentScanPreviewPresentationRequested = false
16
- private var documentScanPreviewPresentationGeneration = 0
17
- private var documentScanShouldCustomizePreviewNavigation = false
18
- private var documentScanPreviewNavigationCustomized = false
19
- private var documentScanModalSuppressionGeneration = 0
20
-
21
6
  private protocol SimulatorDocumentScannerViewControllerDelegate: AnyObject {
22
7
  func simulatorDocumentScannerViewControllerDidCancel(_ controller: SimulatorDocumentScannerViewController)
23
8
  func simulatorDocumentScannerViewController(
@@ -161,8 +146,6 @@ class DocScanner: NSObject {
161
146
  private var reviewCapturedDocument: Bool
162
147
  private let ciContext = CIContext()
163
148
 
164
- private static var swizzled = false
165
-
166
149
  init(
167
150
  _ viewController: UIViewController? = nil,
168
151
  successHandler: @escaping ([String]) -> Void = { _ in },
@@ -193,6 +176,14 @@ class DocScanner: NSObject {
193
176
  self.init(nil)
194
177
  }
195
178
 
179
+ private var shouldUseSimulatorHarness: Bool {
180
+ #if targetEnvironment(simulator)
181
+ return true
182
+ #else
183
+ return false
184
+ #endif
185
+ }
186
+
196
187
  func startScan() {
197
188
  guard let viewController else {
198
189
  errorHandler("Bridge view controller unavailable.")
@@ -216,7 +207,7 @@ class DocScanner: NSObject {
216
207
  return
217
208
  }
218
209
 
219
- DocScanner.configureVisionKitHackState(
210
+ VisionKitScanSwizzle.prepare(
220
211
  limit: maxNumDocuments,
221
212
  presentPreviewAfterCapture: reviewCapturedDocument || letUserAdjustCrop
222
213
  )
@@ -224,7 +215,7 @@ class DocScanner: NSObject {
224
215
  DispatchQueue.main.async {
225
216
  let documentCameraViewController = VNDocumentCameraViewController()
226
217
  documentCameraViewController.delegate = self
227
- activeDocumentCameraController.value = documentCameraViewController
218
+ VisionKitScanSwizzle.attach(controller: documentCameraViewController)
228
219
  viewController.present(documentCameraViewController, animated: true)
229
220
  }
230
221
  }
@@ -257,618 +248,6 @@ class DocScanner: NSObject {
257
248
  startScan()
258
249
  }
259
250
 
260
- private static func configureVisionKitHackState(limit: Int?, presentPreviewAfterCapture: Bool) {
261
- documentScanLimit = limit
262
- documentScanShouldPresentPreviewAfterCapture = presentPreviewAfterCapture
263
- documentScanLastPreviewedAcceptedCount = 0
264
- documentScanPreviewPresentationRequested = false
265
- documentScanPreviewPresentationGeneration += 1
266
- documentScanShouldCustomizePreviewNavigation = false
267
- documentScanPreviewNavigationCustomized = false
268
- documentScanModalSuppressionGeneration += 1
269
-
270
- guard limit != nil || presentPreviewAfterCapture else {
271
- return
272
- }
273
-
274
- setupSwizzling()
275
- }
276
-
277
- private static func resetVisionKitHackState() {
278
- documentScanLimit = nil
279
- documentScanShouldPresentPreviewAfterCapture = false
280
- documentScanLastPreviewedAcceptedCount = 0
281
- documentScanPreviewPresentationRequested = false
282
- documentScanPreviewPresentationGeneration += 1
283
- documentScanShouldCustomizePreviewNavigation = false
284
- documentScanPreviewNavigationCustomized = false
285
- documentScanModalSuppressionGeneration += 1
286
- activeDocumentCameraController.value = nil
287
- }
288
-
289
- private static func setupSwizzling() {
290
- guard !swizzled else {
291
- return
292
- }
293
-
294
- // appstore-2.5.2-allow: resolve VisionKit in-process document camera class for page-limit hook
295
- guard let inProcessClass = NSClassFromString(VisionKitPrivateConstants.inProcessViewControllerClassName) else {
296
- return
297
- }
298
-
299
- let originalSelector = VisionKitPrivateConstants.documentCameraCanAddImagesSelector
300
- let swizzledSelector = #selector(DocScanner.swizzled_documentCameraController(_:canAddImages:))
301
-
302
- guard
303
- // appstore-2.5.2-allow: read private canAddImages implementation before swizzle
304
- let originalMethod = class_getInstanceMethod(inProcessClass, originalSelector),
305
- let swizzledMethod = class_getInstanceMethod(DocScanner.self, swizzledSelector)
306
- else {
307
- return
308
- }
309
-
310
- swizzled = true
311
- // appstore-2.5.2-allow: install swizzled canAddImages handler on in-process VisionKit class
312
- let didAddMethod = class_addMethod(
313
- inProcessClass,
314
- swizzledSelector,
315
- method_getImplementation(swizzledMethod),
316
- method_getTypeEncoding(swizzledMethod)
317
- )
318
-
319
- if didAddMethod,
320
- let installedSwizzledMethod = class_getInstanceMethod(inProcessClass, swizzledSelector) {
321
- // appstore-2.5.2-allow: exchange canAddImages implementations for scan limit enforcement
322
- method_exchangeImplementations(originalMethod, installedSwizzledMethod)
323
- return
324
- }
325
-
326
- // appstore-2.5.2-allow: exchange canAddImages implementations for scan limit enforcement
327
- method_exchangeImplementations(originalMethod, swizzledMethod)
328
- }
329
-
330
- @objc dynamic func swizzled_documentCameraController(_ controller: AnyObject, canAddImages count: UInt64) -> Bool {
331
- let originalAllowsMoreImages = swizzled_documentCameraController(controller, canAddImages: count)
332
- let acceptedCount = max(0, Int(count) - 1)
333
- let reachedConfiguredLimit = documentScanLimit.map { Int(count) > $0 } ?? false
334
- var previewWasRequested = false
335
-
336
- if documentScanShouldPresentPreviewAfterCapture,
337
- acceptedCount > 0,
338
- acceptedCount > documentScanLastPreviewedAcceptedCount {
339
- documentScanLastPreviewedAcceptedCount = acceptedCount
340
- previewWasRequested = true
341
- DocScanner.requestPreviewPresentationIfNeeded(
342
- forceNewCycle: true,
343
- customizeForCompletion: reachedConfiguredLimit
344
- )
345
- }
346
-
347
- if reachedConfiguredLimit {
348
- if !previewWasRequested {
349
- DocScanner.requestPreviewPresentationIfNeeded(
350
- forceNewCycle: true,
351
- customizeForCompletion: true
352
- )
353
- }
354
- DocScanner.suppressLimitModalIfNeeded()
355
- return false
356
- }
357
-
358
- return originalAllowsMoreImages
359
- }
360
-
361
- private var shouldUseSimulatorHarness: Bool {
362
- #if targetEnvironment(simulator)
363
- return true
364
- #else
365
- return false
366
- #endif
367
- }
368
-
369
- private static func resolvedView(for controller: UIViewController) -> UIView? {
370
- controller.viewIfLoaded ?? controller.view
371
- }
372
-
373
- private static func requestPreviewPresentationIfNeeded(forceNewCycle: Bool, customizeForCompletion: Bool) {
374
- DispatchQueue.main.async {
375
- guard let documentCameraViewController = activeDocumentCameraController.value else {
376
- return
377
- }
378
-
379
- if forceNewCycle {
380
- documentScanPreviewPresentationRequested = false
381
- documentScanPreviewNavigationCustomized = false
382
- }
383
-
384
- documentScanShouldCustomizePreviewNavigation =
385
- documentScanShouldCustomizePreviewNavigation || customizeForCompletion
386
-
387
- guard !documentScanPreviewPresentationRequested else {
388
- if documentScanShouldCustomizePreviewNavigation {
389
- _ = customizePreviewNavigationIfNeeded(around: documentCameraViewController)
390
- }
391
- return
392
- }
393
-
394
- documentScanPreviewPresentationRequested = true
395
- let generation = documentScanPreviewPresentationGeneration + 1
396
- documentScanPreviewPresentationGeneration = generation
397
- attemptPreviewPresentation(generation: generation, remainingAttempts: 12)
398
- }
399
- }
400
-
401
- private static func attemptPreviewPresentation(generation: Int, remainingAttempts: Int) {
402
- DispatchQueue.main.async {
403
- guard
404
- generation == documentScanPreviewPresentationGeneration,
405
- let documentCameraViewController = activeDocumentCameraController.value
406
- else {
407
- return
408
- }
409
-
410
- if findVisiblePreviewOrEditorController(around: documentCameraViewController) != nil {
411
- if documentScanShouldCustomizePreviewNavigation {
412
- _ = customizePreviewNavigationIfNeeded(around: documentCameraViewController)
413
- }
414
- return
415
- }
416
-
417
- guard let rootView = resolvedView(for: documentCameraViewController) else {
418
- return
419
- }
420
-
421
- let previewWasTriggered: Bool
422
- if let previewView = findFirstView(
423
- namedLike: [VisionKitPrivateConstants.thumbnailContainerViewClassFragment],
424
- in: rootView
425
- ) {
426
- previewWasTriggered = triggerInteraction(around: previewView, in: rootView)
427
- } else {
428
- let previewPoint = CGPoint(
429
- x: previewHotspotFrame(in: rootView).midX,
430
- y: previewHotspotFrame(in: rootView).midY
431
- )
432
- previewWasTriggered = triggerInteraction(at: previewPoint, in: rootView)
433
- }
434
-
435
- if !previewWasTriggered && remainingAttempts <= 0 {
436
- documentScanPreviewPresentationRequested = false
437
- return
438
- }
439
-
440
- guard remainingAttempts > 0 else {
441
- return
442
- }
443
-
444
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.08) {
445
- attemptPreviewPresentation(generation: generation, remainingAttempts: remainingAttempts - 1)
446
- }
447
- }
448
- }
449
-
450
- @discardableResult
451
- private static func customizePreviewNavigationIfNeeded(around scannerController: UIViewController) -> Bool {
452
- guard documentScanShouldCustomizePreviewNavigation else {
453
- return false
454
- }
455
-
456
- guard let previewController = findVisiblePreviewOrEditorController(around: scannerController) else {
457
- return false
458
- }
459
-
460
- guard !documentScanPreviewNavigationCustomized else {
461
- return true
462
- }
463
-
464
- let completionButton = findCompletionButton(in: scannerController)
465
- let completionTitle = completionButton?.title ?? "Done"
466
- let completionStyle = completionButton?.style == .plain ? UIBarButtonItem.Style.plain : .done
467
-
468
- previewController.navigationItem.hidesBackButton = true
469
- previewController.navigationItem.setHidesBackButton(true, animated: false)
470
- previewController.navigationItem.leftItemsSupplementBackButton = false
471
-
472
- if let completionAction = completionButton?.action {
473
- previewController.navigationItem.leftBarButtonItem = UIBarButtonItem(
474
- title: completionTitle,
475
- style: completionStyle,
476
- target: completionButton?.target,
477
- action: completionAction
478
- )
479
- }
480
-
481
- documentScanPreviewNavigationCustomized = true
482
- return true
483
- }
484
-
485
- private static func triggerInteraction(around view: UIView, in rootView: UIView) -> Bool {
486
- var currentView: UIView? = view
487
-
488
- while let candidate = currentView {
489
- if triggerInteraction(on: candidate) {
490
- return true
491
- }
492
- currentView = candidate.superview
493
- }
494
-
495
- let targetPoint = view.convert(CGPoint(x: view.bounds.midX, y: view.bounds.midY), to: rootView)
496
- return triggerInteraction(at: targetPoint, in: rootView)
497
- }
498
-
499
- private static func triggerInteraction(at point: CGPoint, in rootView: UIView) -> Bool {
500
- guard let hitView = rootView.hitTest(point, with: nil) else {
501
- return false
502
- }
503
-
504
- var currentView: UIView? = hitView
505
- while let candidate = currentView {
506
- if triggerInteraction(on: candidate) {
507
- return true
508
- }
509
- currentView = candidate.superview
510
- }
511
-
512
- return false
513
- }
514
-
515
- private static func triggerInteraction(on view: UIView) -> Bool {
516
- if let control = view as? UIControl {
517
- triggerBestPrimaryAction(for: control)
518
- return true
519
- }
520
-
521
- return triggerGestureRecognizers(on: view)
522
- }
523
-
524
- private static func triggerGestureRecognizers(on view: UIView) -> Bool {
525
- guard let gestureRecognizers = view.gestureRecognizers, !gestureRecognizers.isEmpty else {
526
- return false
527
- }
528
-
529
- for gestureRecognizer in gestureRecognizers where gestureRecognizer.isEnabled {
530
- // appstore-2.5.2-allow: read UIKit gesture recognizer target list to trigger preview navigation
531
- guard let internalTargets = gestureRecognizer.value(
532
- forKey: VisionKitPrivateConstants.gestureRecognizerTargetsKey
533
- ) as? [NSObject] else {
534
- continue
535
- }
536
-
537
- for internalTarget in internalTargets {
538
- guard let invocation = gestureRecognizerInvocation(from: internalTarget) else {
539
- continue
540
- }
541
-
542
- UIApplication.shared.sendAction(
543
- invocation.selector,
544
- to: invocation.target,
545
- from: gestureRecognizer,
546
- for: nil
547
- )
548
- return true
549
- }
550
- }
551
-
552
- return false
553
- }
554
-
555
- private static func gestureRecognizerInvocation(from internalTarget: NSObject) -> (target: AnyObject, selector: Selector)? {
556
- guard let targetClass: AnyClass = object_getClass(internalTarget) else {
557
- return nil
558
- }
559
-
560
- // appstore-2.5.2-allow: read UIKit gesture target/action ivars to forward preview taps
561
- guard
562
- let targetIvar = class_getInstanceVariable(
563
- targetClass,
564
- VisionKitPrivateConstants.gestureTargetIvarName
565
- ),
566
- let actionIvar = class_getInstanceVariable(
567
- targetClass,
568
- VisionKitPrivateConstants.gestureActionIvarName
569
- ),
570
- // appstore-2.5.2-allow: read gesture target object from UIKit internal target entry
571
- let target = object_getIvar(internalTarget, targetIvar) as AnyObject?
572
- else {
573
- return nil
574
- }
575
-
576
- // appstore-2.5.2-allow: read gesture action selector offset from UIKit internal target entry
577
- let actionOffset = ivar_getOffset(actionIvar)
578
- let actionPointer = Unmanaged.passUnretained(internalTarget).toOpaque().advanced(by: actionOffset)
579
- let selector = actionPointer.load(as: Selector.self)
580
-
581
- guard let responder = target as? NSObject,
582
- responder.responds(to: selector) else {
583
- return nil
584
- }
585
-
586
- return (target, selector)
587
- }
588
-
589
- private static func triggerBestPrimaryAction(for control: UIControl) {
590
- let supportedEvents: [UIControl.Event] = [.primaryActionTriggered, .touchUpInside, .valueChanged]
591
-
592
- for event in supportedEvents {
593
- for target in control.allTargets {
594
- if let actions = control.actions(forTarget: target, forControlEvent: event),
595
- !actions.isEmpty {
596
- control.sendActions(for: event)
597
- return
598
- }
599
- }
600
- }
601
-
602
- control.sendActions(for: .touchUpInside)
603
- }
604
-
605
- private static func findFirstView(namedLike classNameFragments: [String], in rootView: UIView) -> UIView? {
606
- findFirstView(namedLike: classNameFragments, in: rootView, rootView: rootView)
607
- }
608
-
609
- private static func findFirstView(
610
- namedLike classNameFragments: [String],
611
- in view: UIView,
612
- rootView: UIView
613
- ) -> UIView? {
614
- guard !view.isHidden, view.alpha > 0.01 else {
615
- return nil
616
- }
617
-
618
- let className = NSStringFromClass(type(of: view))
619
- if classNameFragments.contains(where: { className.contains($0) }) {
620
- return view
621
- }
622
-
623
- for subview in view.subviews {
624
- if let matchingSubview = findFirstView(namedLike: classNameFragments, in: subview, rootView: rootView) {
625
- return matchingSubview
626
- }
627
- }
628
-
629
- return nil
630
- }
631
-
632
- private static func findVisiblePreviewOrEditorController(around scannerController: UIViewController) -> UIViewController? {
633
- var controllersToVisit: [UIViewController] = []
634
- var visitedControllers = Set<ObjectIdentifier>()
635
-
636
- if let navigationController = scannerController.navigationController {
637
- if let topViewController = navigationController.topViewController,
638
- topViewController !== scannerController,
639
- topViewController.isViewLoaded,
640
- topViewController.view.window != nil {
641
- return topViewController
642
- }
643
- controllersToVisit.append(navigationController)
644
- }
645
-
646
- controllersToVisit.append(contentsOf: scannerController.children)
647
-
648
- if let presentedViewController = scannerController.presentedViewController {
649
- controllersToVisit.append(presentedViewController)
650
- }
651
-
652
- while !controllersToVisit.isEmpty {
653
- let currentController = controllersToVisit.removeFirst()
654
- let identifier = ObjectIdentifier(currentController)
655
-
656
- guard visitedControllers.insert(identifier).inserted else {
657
- continue
658
- }
659
-
660
- if currentController !== scannerController,
661
- currentController.isViewLoaded,
662
- currentController.view.window != nil,
663
- looksLikePreviewOrEditorController(currentController) {
664
- return currentController
665
- }
666
-
667
- if let navigationController = currentController as? UINavigationController {
668
- if let topViewController = navigationController.topViewController,
669
- topViewController !== scannerController,
670
- topViewController.isViewLoaded,
671
- topViewController.view.window != nil {
672
- return topViewController
673
- }
674
- controllersToVisit.append(contentsOf: navigationController.viewControllers)
675
- }
676
-
677
- controllersToVisit.append(contentsOf: currentController.children)
678
-
679
- if let presentedViewController = currentController.presentedViewController {
680
- controllersToVisit.append(presentedViewController)
681
- }
682
- }
683
-
684
- return nil
685
- }
686
-
687
- private static func looksLikePreviewOrEditorController(_ viewController: UIViewController) -> Bool {
688
- let className = NSStringFromClass(type(of: viewController)).lowercased()
689
- let previewKeywords = ["preview", "review", "edit", "editor", "filter", "rotate", "crop"]
690
- return previewKeywords.contains { className.contains($0) }
691
- }
692
-
693
- private static func findCompletionButton(in rootViewController: UIViewController) -> UIBarButtonItem? {
694
- var controllersToVisit: [UIViewController] = [rootViewController]
695
- var visitedControllers = Set<ObjectIdentifier>()
696
- var fallbackButton: UIBarButtonItem?
697
-
698
- while !controllersToVisit.isEmpty {
699
- let currentController = controllersToVisit.removeFirst()
700
- let identifier = ObjectIdentifier(currentController)
701
-
702
- guard visitedControllers.insert(identifier).inserted else {
703
- continue
704
- }
705
-
706
- let rightBarButtonItems = currentController.navigationItem.rightBarButtonItems ?? []
707
- for barButtonItem in rightBarButtonItems where barButtonItem.isEnabled && barButtonItem.action != nil {
708
- if isCompletionButton(barButtonItem) {
709
- return barButtonItem
710
- }
711
- fallbackButton = fallbackButton ?? barButtonItem
712
- }
713
-
714
- if let rightBarButtonItem = currentController.navigationItem.rightBarButtonItem,
715
- rightBarButtonItem.isEnabled,
716
- rightBarButtonItem.action != nil {
717
- if isCompletionButton(rightBarButtonItem) {
718
- return rightBarButtonItem
719
- }
720
- fallbackButton = fallbackButton ?? rightBarButtonItem
721
- }
722
-
723
- if let navigationController = currentController as? UINavigationController {
724
- controllersToVisit.append(contentsOf: navigationController.viewControllers)
725
- }
726
-
727
- controllersToVisit.append(contentsOf: currentController.children)
728
-
729
- if let parentController = currentController.parent {
730
- controllersToVisit.append(parentController)
731
- }
732
-
733
- if let presentedViewController = currentController.presentedViewController {
734
- controllersToVisit.append(presentedViewController)
735
- }
736
- }
737
-
738
- return fallbackButton
739
- }
740
-
741
- private static func isCompletionButton(_ barButtonItem: UIBarButtonItem) -> Bool {
742
- if barButtonItem.style == .done {
743
- return true
744
- }
745
-
746
- let normalizedTitle = normalizeActionText(barButtonItem.title)
747
- return normalizedTitle == "done" || normalizedTitle == "save" || normalizedTitle == "done scanning"
748
- }
749
-
750
- private static func normalizeActionText(_ text: String?) -> String {
751
- text?
752
- .trimmingCharacters(in: .whitespacesAndNewlines)
753
- .folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current)
754
- .lowercased() ?? ""
755
- }
756
-
757
- private static func previewHotspotFrame(in rootView: UIView) -> CGRect {
758
- CGRect(
759
- x: 0,
760
- y: rootView.bounds.height * 0.56,
761
- width: rootView.bounds.width * 0.42,
762
- height: rootView.bounds.height * 0.44
763
- )
764
- }
765
-
766
- private static func suppressLimitModalIfNeeded() {
767
- let generation = documentScanModalSuppressionGeneration + 1
768
- documentScanModalSuppressionGeneration = generation
769
- suppressLimitModalIfNeeded(generation: generation, remainingAttempts: 12)
770
- }
771
-
772
- private static func suppressLimitModalIfNeeded(generation: Int, remainingAttempts: Int) {
773
- DispatchQueue.main.async {
774
- guard generation == documentScanModalSuppressionGeneration else {
775
- return
776
- }
777
-
778
- dismissVisibleLimitModalIfNeeded()
779
-
780
- guard remainingAttempts > 0 else {
781
- return
782
- }
783
-
784
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
785
- suppressLimitModalIfNeeded(generation: generation, remainingAttempts: remainingAttempts - 1)
786
- }
787
- }
788
- }
789
-
790
- private static func dismissVisibleLimitModalIfNeeded() {
791
- guard let documentCameraViewController = activeDocumentCameraController.value else {
792
- return
793
- }
794
-
795
- let modalsToDismiss = findPresentedModals(around: documentCameraViewController)
796
- for modal in modalsToDismiss {
797
- modal.view.alpha = 0
798
- modal.view.isHidden = true
799
- modal.dismiss(animated: false)
800
- }
801
- }
802
-
803
- private static func findPresentedModals(around rootViewController: UIViewController) -> [UIViewController] {
804
- var results: [UIViewController] = []
805
- var controllersToVisit: [UIViewController] = [rootViewController]
806
- var visitedControllers = Set<ObjectIdentifier>()
807
- var seenResults = Set<ObjectIdentifier>()
808
-
809
- while !controllersToVisit.isEmpty {
810
- let currentController = controllersToVisit.removeFirst()
811
- let identifier = ObjectIdentifier(currentController)
812
-
813
- guard visitedControllers.insert(identifier).inserted else {
814
- continue
815
- }
816
-
817
- if let presentedViewController = currentController.presentedViewController,
818
- shouldSuppressModal(presentedViewController, scannerRoot: rootViewController),
819
- seenResults.insert(ObjectIdentifier(presentedViewController)).inserted {
820
- results.append(presentedViewController)
821
- }
822
-
823
- if let navigationController = currentController as? UINavigationController {
824
- controllersToVisit.append(contentsOf: navigationController.viewControllers)
825
- }
826
-
827
- controllersToVisit.append(contentsOf: currentController.children)
828
- }
829
-
830
- if let windowScene = rootViewController.view.window?.windowScene {
831
- for window in windowScene.windows {
832
- guard let windowRootViewController = window.rootViewController else {
833
- continue
834
- }
835
-
836
- if let modal = topPresentedViewController(from: windowRootViewController),
837
- shouldSuppressModal(modal, scannerRoot: rootViewController),
838
- seenResults.insert(ObjectIdentifier(modal)).inserted {
839
- results.append(modal)
840
- }
841
- }
842
- }
843
-
844
- return results
845
- }
846
-
847
- private static func topPresentedViewController(from rootViewController: UIViewController) -> UIViewController? {
848
- var currentViewController: UIViewController? = rootViewController
849
- var lastPresentedViewController: UIViewController?
850
-
851
- while let presentedViewController = currentViewController?.presentedViewController {
852
- lastPresentedViewController = presentedViewController
853
- currentViewController = presentedViewController
854
- }
855
-
856
- return lastPresentedViewController
857
- }
858
-
859
- private static func shouldSuppressModal(_ viewController: UIViewController, scannerRoot: UIViewController) -> Bool {
860
- guard viewController !== scannerRoot else {
861
- return false
862
- }
863
-
864
- if viewController is UIAlertController {
865
- return true
866
- }
867
-
868
- let className = NSStringFromClass(type(of: viewController)).lowercased()
869
- return className.contains("alert") || className.contains("sheet") || className.contains("prompt")
870
- }
871
-
872
251
  private func finishScan(with scan: VNDocumentCameraScan) {
873
252
  processImages(pageCount: limitedPageCount(total: scan.pageCount)) { index in
874
253
  scan.imageOfPage(at: index)
@@ -1033,14 +412,14 @@ extension DocScanner: VNDocumentCameraViewControllerDelegate {
1033
412
  _ controller: VNDocumentCameraViewController,
1034
413
  didFinishWith scan: VNDocumentCameraScan
1035
414
  ) {
1036
- DocScanner.resetVisionKitHackState()
415
+ VisionKitScanSwizzle.reset()
1037
416
  dismiss(controller) {
1038
417
  self.finishScan(with: scan)
1039
418
  }
1040
419
  }
1041
420
 
1042
421
  func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
1043
- DocScanner.resetVisionKitHackState()
422
+ VisionKitScanSwizzle.reset()
1044
423
  dismiss(controller) {
1045
424
  self.cancelHandler()
1046
425
  }
@@ -1050,7 +429,7 @@ extension DocScanner: VNDocumentCameraViewControllerDelegate {
1050
429
  _ controller: VNDocumentCameraViewController,
1051
430
  didFailWithError error: Error
1052
431
  ) {
1053
- DocScanner.resetVisionKitHackState()
432
+ VisionKitScanSwizzle.reset()
1054
433
  dismiss(controller) {
1055
434
  self.errorHandler(error.localizedDescription)
1056
435
  }