@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.
- package/CapgoCapacitorDocumentScanner.podspec +8 -0
- package/Package.swift +11 -2
- package/README.md +52 -12
- package/dist/docs.json +3 -3
- package/dist/esm/definitions.d.ts +14 -7
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/DocumentScannerPlugin/DocScanner.swift +14 -635
- package/ios/Sources/DocumentScannerPlugin/DocumentScannerPlugin.swift +1 -1
- package/ios/Sources/DocumentScannerPlugin/VisionKitScanSwizzle.swift +675 -0
- package/ios/Tests/DocumentScannerPluginTests/DocumentScannerPluginTests.swift +6 -1
- package/package.json +5 -3
- package/scripts/configure-visionkit-swizzle.mjs +66 -0
- package/ios/Sources/DocumentScannerPlugin/VisionKitPrivateConstants.swift +0 -10
|
@@ -3,7 +3,7 @@ import Foundation
|
|
|
3
3
|
|
|
4
4
|
@objc(DocumentScannerPlugin)
|
|
5
5
|
public class DocumentScannerPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
6
|
-
private let pluginVersion: String = "8.4.
|
|
6
|
+
private let pluginVersion: String = "8.4.15"
|
|
7
7
|
public let identifier = "DocumentScannerPlugin"
|
|
8
8
|
public let jsName = "DocumentScanner"
|
|
9
9
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
// swiftlint:disable file_length type_body_length identifier_name
|
|
2
|
+
import Foundation
|
|
3
|
+
import UIKit
|
|
4
|
+
import VisionKit
|
|
5
|
+
|
|
6
|
+
#if CAPGO_DOCUMENT_SCANNER_VISIONKIT_SWIZZLE
|
|
7
|
+
import ObjectiveC.runtime
|
|
8
|
+
|
|
9
|
+
enum VisionKitPrivateConstants {
|
|
10
|
+
static let inProcessViewControllerClassName = "VNDocumentCameraViewController_InProcess"
|
|
11
|
+
static let documentCameraCanAddImagesSelector = Selector("documentCameraController:canAddImages:")
|
|
12
|
+
static let gestureRecognizerTargetsKey = "_targets"
|
|
13
|
+
static let gestureTargetIvarName = "_target"
|
|
14
|
+
static let gestureActionIvarName = "_action"
|
|
15
|
+
static let thumbnailContainerViewClassFragment = "ICDocCamThumbnailContainerView"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private final class WeakDocumentCameraController {
|
|
19
|
+
weak var value: VNDocumentCameraViewController?
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private let activeDocumentCameraController = WeakDocumentCameraController()
|
|
23
|
+
private var documentScanLimit: Int?
|
|
24
|
+
private var documentScanShouldPresentPreviewAfterCapture = false
|
|
25
|
+
private var documentScanLastPreviewedAcceptedCount = 0
|
|
26
|
+
private var documentScanPreviewPresentationRequested = false
|
|
27
|
+
private var documentScanPreviewPresentationGeneration = 0
|
|
28
|
+
private var documentScanShouldCustomizePreviewNavigation = false
|
|
29
|
+
private var documentScanPreviewNavigationCustomized = false
|
|
30
|
+
private var documentScanModalSuppressionGeneration = 0
|
|
31
|
+
|
|
32
|
+
final class VisionKitScanSwizzle: NSObject {
|
|
33
|
+
static let isCompiledIn = true
|
|
34
|
+
private static var swizzled = false
|
|
35
|
+
|
|
36
|
+
static func prepare(limit: Int?, presentPreviewAfterCapture: Bool) {
|
|
37
|
+
documentScanLimit = limit
|
|
38
|
+
documentScanShouldPresentPreviewAfterCapture = presentPreviewAfterCapture
|
|
39
|
+
documentScanLastPreviewedAcceptedCount = 0
|
|
40
|
+
documentScanPreviewPresentationRequested = false
|
|
41
|
+
documentScanPreviewPresentationGeneration += 1
|
|
42
|
+
documentScanShouldCustomizePreviewNavigation = false
|
|
43
|
+
documentScanPreviewNavigationCustomized = false
|
|
44
|
+
documentScanModalSuppressionGeneration += 1
|
|
45
|
+
|
|
46
|
+
guard limit != nil || presentPreviewAfterCapture else {
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
setupSwizzling()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static func reset() {
|
|
54
|
+
documentScanLimit = nil
|
|
55
|
+
documentScanShouldPresentPreviewAfterCapture = false
|
|
56
|
+
documentScanLastPreviewedAcceptedCount = 0
|
|
57
|
+
documentScanPreviewPresentationRequested = false
|
|
58
|
+
documentScanPreviewPresentationGeneration += 1
|
|
59
|
+
documentScanShouldCustomizePreviewNavigation = false
|
|
60
|
+
documentScanPreviewNavigationCustomized = false
|
|
61
|
+
documentScanModalSuppressionGeneration += 1
|
|
62
|
+
activeDocumentCameraController.value = nil
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private static func setupSwizzling() {
|
|
66
|
+
guard !swizzled else {
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// appstore-2.5.2-allow: resolve VisionKit in-process document camera class for page-limit swizzle
|
|
71
|
+
guard let inProcessClass = NSClassFromString(VisionKitPrivateConstants.inProcessViewControllerClassName) else {
|
|
72
|
+
return
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let originalSelector = VisionKitPrivateConstants.documentCameraCanAddImagesSelector
|
|
76
|
+
let swizzledSelector = #selector(VisionKitScanSwizzle.swizzled_documentCameraController(_:canAddImages:))
|
|
77
|
+
|
|
78
|
+
guard
|
|
79
|
+
// appstore-2.5.2-allow: read private canAddImages implementation before swizzle
|
|
80
|
+
let originalMethod = class_getInstanceMethod(inProcessClass, originalSelector),
|
|
81
|
+
let swizzledMethod = class_getInstanceMethod(VisionKitScanSwizzle.self, swizzledSelector)
|
|
82
|
+
else {
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
swizzled = true
|
|
87
|
+
// appstore-2.5.2-allow: install swizzled canAddImages handler on in-process VisionKit class
|
|
88
|
+
let didAddMethod = class_addMethod(
|
|
89
|
+
inProcessClass,
|
|
90
|
+
swizzledSelector,
|
|
91
|
+
method_getImplementation(swizzledMethod),
|
|
92
|
+
method_getTypeEncoding(swizzledMethod)
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
if didAddMethod,
|
|
96
|
+
let installedSwizzledMethod = class_getInstanceMethod(inProcessClass, swizzledSelector) {
|
|
97
|
+
// appstore-2.5.2-allow: exchange canAddImages implementations for scan limit enforcement
|
|
98
|
+
method_exchangeImplementations(originalMethod, installedSwizzledMethod)
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// appstore-2.5.2-allow: exchange canAddImages implementations for scan limit enforcement
|
|
103
|
+
method_exchangeImplementations(originalMethod, swizzledMethod)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
@objc dynamic func swizzled_documentCameraController(_ controller: AnyObject, canAddImages count: UInt64) -> Bool {
|
|
107
|
+
let originalAllowsMoreImages = swizzled_documentCameraController(controller, canAddImages: count)
|
|
108
|
+
let acceptedCount = max(0, Int(count) - 1)
|
|
109
|
+
let reachedConfiguredLimit = documentScanLimit.map { Int(count) > $0 } ?? false
|
|
110
|
+
var previewWasRequested = false
|
|
111
|
+
|
|
112
|
+
if documentScanShouldPresentPreviewAfterCapture,
|
|
113
|
+
acceptedCount > 0,
|
|
114
|
+
acceptedCount > documentScanLastPreviewedAcceptedCount {
|
|
115
|
+
documentScanLastPreviewedAcceptedCount = acceptedCount
|
|
116
|
+
previewWasRequested = true
|
|
117
|
+
VisionKitScanSwizzle.requestPreviewPresentationIfNeeded(
|
|
118
|
+
forceNewCycle: true,
|
|
119
|
+
customizeForCompletion: reachedConfiguredLimit
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if reachedConfiguredLimit {
|
|
124
|
+
if !previewWasRequested {
|
|
125
|
+
VisionKitScanSwizzle.requestPreviewPresentationIfNeeded(
|
|
126
|
+
forceNewCycle: true,
|
|
127
|
+
customizeForCompletion: true
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
VisionKitScanSwizzle.suppressLimitModalIfNeeded()
|
|
131
|
+
return false
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return originalAllowsMoreImages
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private static func resolvedView(for controller: UIViewController) -> UIView? {
|
|
138
|
+
controller.viewIfLoaded ?? controller.view
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private static func requestPreviewPresentationIfNeeded(forceNewCycle: Bool, customizeForCompletion: Bool) {
|
|
142
|
+
DispatchQueue.main.async {
|
|
143
|
+
guard let documentCameraViewController = activeDocumentCameraController.value else {
|
|
144
|
+
return
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if forceNewCycle {
|
|
148
|
+
documentScanPreviewPresentationRequested = false
|
|
149
|
+
documentScanPreviewNavigationCustomized = false
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
documentScanShouldCustomizePreviewNavigation =
|
|
153
|
+
documentScanShouldCustomizePreviewNavigation || customizeForCompletion
|
|
154
|
+
|
|
155
|
+
guard !documentScanPreviewPresentationRequested else {
|
|
156
|
+
if documentScanShouldCustomizePreviewNavigation {
|
|
157
|
+
_ = customizePreviewNavigationIfNeeded(around: documentCameraViewController)
|
|
158
|
+
}
|
|
159
|
+
return
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
documentScanPreviewPresentationRequested = true
|
|
163
|
+
let generation = documentScanPreviewPresentationGeneration + 1
|
|
164
|
+
documentScanPreviewPresentationGeneration = generation
|
|
165
|
+
attemptPreviewPresentation(generation: generation, remainingAttempts: 12)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private static func attemptPreviewPresentation(generation: Int, remainingAttempts: Int) {
|
|
170
|
+
DispatchQueue.main.async {
|
|
171
|
+
guard
|
|
172
|
+
generation == documentScanPreviewPresentationGeneration,
|
|
173
|
+
let documentCameraViewController = activeDocumentCameraController.value
|
|
174
|
+
else {
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if findVisiblePreviewOrEditorController(around: documentCameraViewController) != nil {
|
|
179
|
+
if documentScanShouldCustomizePreviewNavigation {
|
|
180
|
+
_ = customizePreviewNavigationIfNeeded(around: documentCameraViewController)
|
|
181
|
+
}
|
|
182
|
+
return
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
guard let rootView = resolvedView(for: documentCameraViewController) else {
|
|
186
|
+
return
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
let previewWasTriggered: Bool
|
|
190
|
+
if let previewView = findFirstView(
|
|
191
|
+
namedLike: [VisionKitPrivateConstants.thumbnailContainerViewClassFragment],
|
|
192
|
+
in: rootView
|
|
193
|
+
) {
|
|
194
|
+
previewWasTriggered = triggerInteraction(around: previewView, in: rootView)
|
|
195
|
+
} else {
|
|
196
|
+
let previewPoint = CGPoint(
|
|
197
|
+
x: previewHotspotFrame(in: rootView).midX,
|
|
198
|
+
y: previewHotspotFrame(in: rootView).midY
|
|
199
|
+
)
|
|
200
|
+
previewWasTriggered = triggerInteraction(at: previewPoint, in: rootView)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if !previewWasTriggered && remainingAttempts <= 0 {
|
|
204
|
+
documentScanPreviewPresentationRequested = false
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
guard remainingAttempts > 0 else {
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.08) {
|
|
213
|
+
attemptPreviewPresentation(generation: generation, remainingAttempts: remainingAttempts - 1)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
@discardableResult
|
|
219
|
+
private static func customizePreviewNavigationIfNeeded(around scannerController: UIViewController) -> Bool {
|
|
220
|
+
guard documentScanShouldCustomizePreviewNavigation else {
|
|
221
|
+
return false
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
guard let previewController = findVisiblePreviewOrEditorController(around: scannerController) else {
|
|
225
|
+
return false
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
guard !documentScanPreviewNavigationCustomized else {
|
|
229
|
+
return true
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
let completionButton = findCompletionButton(in: scannerController)
|
|
233
|
+
let completionTitle = completionButton?.title ?? "Done"
|
|
234
|
+
let completionStyle = completionButton?.style == .plain ? UIBarButtonItem.Style.plain : .done
|
|
235
|
+
|
|
236
|
+
previewController.navigationItem.hidesBackButton = true
|
|
237
|
+
previewController.navigationItem.setHidesBackButton(true, animated: false)
|
|
238
|
+
previewController.navigationItem.leftItemsSupplementBackButton = false
|
|
239
|
+
|
|
240
|
+
if let completionAction = completionButton?.action {
|
|
241
|
+
previewController.navigationItem.leftBarButtonItem = UIBarButtonItem(
|
|
242
|
+
title: completionTitle,
|
|
243
|
+
style: completionStyle,
|
|
244
|
+
target: completionButton?.target,
|
|
245
|
+
action: completionAction
|
|
246
|
+
)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
documentScanPreviewNavigationCustomized = true
|
|
250
|
+
return true
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private static func triggerInteraction(around view: UIView, in rootView: UIView) -> Bool {
|
|
254
|
+
var currentView: UIView? = view
|
|
255
|
+
|
|
256
|
+
while let candidate = currentView {
|
|
257
|
+
if triggerInteraction(on: candidate) {
|
|
258
|
+
return true
|
|
259
|
+
}
|
|
260
|
+
currentView = candidate.superview
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let targetPoint = view.convert(CGPoint(x: view.bounds.midX, y: view.bounds.midY), to: rootView)
|
|
264
|
+
return triggerInteraction(at: targetPoint, in: rootView)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private static func triggerInteraction(at point: CGPoint, in rootView: UIView) -> Bool {
|
|
268
|
+
guard let hitView = rootView.hitTest(point, with: nil) else {
|
|
269
|
+
return false
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
var currentView: UIView? = hitView
|
|
273
|
+
while let candidate = currentView {
|
|
274
|
+
if triggerInteraction(on: candidate) {
|
|
275
|
+
return true
|
|
276
|
+
}
|
|
277
|
+
currentView = candidate.superview
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return false
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private static func triggerInteraction(on view: UIView) -> Bool {
|
|
284
|
+
if let control = view as? UIControl {
|
|
285
|
+
triggerBestPrimaryAction(for: control)
|
|
286
|
+
return true
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return triggerGestureRecognizers(on: view)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private static func triggerGestureRecognizers(on view: UIView) -> Bool {
|
|
293
|
+
guard let gestureRecognizers = view.gestureRecognizers, !gestureRecognizers.isEmpty else {
|
|
294
|
+
return false
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
for gestureRecognizer in gestureRecognizers where gestureRecognizer.isEnabled {
|
|
298
|
+
// appstore-2.5.2-allow: read UIKit gesture recognizer target list to trigger preview navigation
|
|
299
|
+
guard let internalTargets = gestureRecognizer.value(
|
|
300
|
+
forKey: VisionKitPrivateConstants.gestureRecognizerTargetsKey
|
|
301
|
+
) as? [NSObject] else {
|
|
302
|
+
continue
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
for internalTarget in internalTargets {
|
|
306
|
+
guard let invocation = gestureRecognizerInvocation(from: internalTarget) else {
|
|
307
|
+
continue
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
UIApplication.shared.sendAction(
|
|
311
|
+
invocation.selector,
|
|
312
|
+
to: invocation.target,
|
|
313
|
+
from: gestureRecognizer,
|
|
314
|
+
for: nil
|
|
315
|
+
)
|
|
316
|
+
return true
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return false
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private static func gestureRecognizerInvocation(from internalTarget: NSObject) -> (target: AnyObject, selector: Selector)? {
|
|
324
|
+
guard let targetClass: AnyClass = object_getClass(internalTarget) else {
|
|
325
|
+
return nil
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// appstore-2.5.2-allow: read UIKit gesture target/action ivars to forward preview taps
|
|
329
|
+
guard
|
|
330
|
+
let targetIvar = class_getInstanceVariable(
|
|
331
|
+
targetClass,
|
|
332
|
+
VisionKitPrivateConstants.gestureTargetIvarName
|
|
333
|
+
),
|
|
334
|
+
let actionIvar = class_getInstanceVariable(
|
|
335
|
+
targetClass,
|
|
336
|
+
VisionKitPrivateConstants.gestureActionIvarName
|
|
337
|
+
),
|
|
338
|
+
// appstore-2.5.2-allow: read gesture target object from UIKit internal target entry
|
|
339
|
+
let target = object_getIvar(internalTarget, targetIvar) as AnyObject?
|
|
340
|
+
else {
|
|
341
|
+
return nil
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// appstore-2.5.2-allow: read gesture action selector offset from UIKit internal target entry
|
|
345
|
+
let actionOffset = ivar_getOffset(actionIvar)
|
|
346
|
+
let actionPointer = Unmanaged.passUnretained(internalTarget).toOpaque().advanced(by: actionOffset)
|
|
347
|
+
let selector = actionPointer.load(as: Selector.self)
|
|
348
|
+
|
|
349
|
+
guard let responder = target as? NSObject,
|
|
350
|
+
responder.responds(to: selector) else {
|
|
351
|
+
return nil
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return (target, selector)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private static func triggerBestPrimaryAction(for control: UIControl) {
|
|
358
|
+
let supportedEvents: [UIControl.Event] = [.primaryActionTriggered, .touchUpInside, .valueChanged]
|
|
359
|
+
|
|
360
|
+
for event in supportedEvents {
|
|
361
|
+
for target in control.allTargets {
|
|
362
|
+
if let actions = control.actions(forTarget: target, forControlEvent: event),
|
|
363
|
+
!actions.isEmpty {
|
|
364
|
+
control.sendActions(for: event)
|
|
365
|
+
return
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
control.sendActions(for: .touchUpInside)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
private static func findFirstView(namedLike classNameFragments: [String], in rootView: UIView) -> UIView? {
|
|
374
|
+
findFirstView(namedLike: classNameFragments, in: rootView, rootView: rootView)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private static func findFirstView(
|
|
378
|
+
namedLike classNameFragments: [String],
|
|
379
|
+
in view: UIView,
|
|
380
|
+
rootView: UIView
|
|
381
|
+
) -> UIView? {
|
|
382
|
+
guard !view.isHidden, view.alpha > 0.01 else {
|
|
383
|
+
return nil
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
let className = NSStringFromClass(type(of: view))
|
|
387
|
+
if classNameFragments.contains(where: { className.contains($0) }) {
|
|
388
|
+
return view
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
for subview in view.subviews {
|
|
392
|
+
if let matchingSubview = findFirstView(namedLike: classNameFragments, in: subview, rootView: rootView) {
|
|
393
|
+
return matchingSubview
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return nil
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
private static func findVisiblePreviewOrEditorController(around scannerController: UIViewController) -> UIViewController? {
|
|
401
|
+
var controllersToVisit: [UIViewController] = []
|
|
402
|
+
var visitedControllers = Set<ObjectIdentifier>()
|
|
403
|
+
|
|
404
|
+
if let navigationController = scannerController.navigationController {
|
|
405
|
+
if let topViewController = navigationController.topViewController,
|
|
406
|
+
topViewController !== scannerController,
|
|
407
|
+
topViewController.isViewLoaded,
|
|
408
|
+
topViewController.view.window != nil {
|
|
409
|
+
return topViewController
|
|
410
|
+
}
|
|
411
|
+
controllersToVisit.append(navigationController)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
controllersToVisit.append(contentsOf: scannerController.children)
|
|
415
|
+
|
|
416
|
+
if let presentedViewController = scannerController.presentedViewController {
|
|
417
|
+
controllersToVisit.append(presentedViewController)
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
while !controllersToVisit.isEmpty {
|
|
421
|
+
let currentController = controllersToVisit.removeFirst()
|
|
422
|
+
let identifier = ObjectIdentifier(currentController)
|
|
423
|
+
|
|
424
|
+
guard visitedControllers.insert(identifier).inserted else {
|
|
425
|
+
continue
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if currentController !== scannerController,
|
|
429
|
+
currentController.isViewLoaded,
|
|
430
|
+
currentController.view.window != nil,
|
|
431
|
+
looksLikePreviewOrEditorController(currentController) {
|
|
432
|
+
return currentController
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if let navigationController = currentController as? UINavigationController {
|
|
436
|
+
if let topViewController = navigationController.topViewController,
|
|
437
|
+
topViewController !== scannerController,
|
|
438
|
+
topViewController.isViewLoaded,
|
|
439
|
+
topViewController.view.window != nil {
|
|
440
|
+
return topViewController
|
|
441
|
+
}
|
|
442
|
+
controllersToVisit.append(contentsOf: navigationController.viewControllers)
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
controllersToVisit.append(contentsOf: currentController.children)
|
|
446
|
+
|
|
447
|
+
if let presentedViewController = currentController.presentedViewController {
|
|
448
|
+
controllersToVisit.append(presentedViewController)
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
return nil
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private static func looksLikePreviewOrEditorController(_ viewController: UIViewController) -> Bool {
|
|
456
|
+
let className = NSStringFromClass(type(of: viewController)).lowercased()
|
|
457
|
+
let previewKeywords = ["preview", "review", "edit", "editor", "filter", "rotate", "crop"]
|
|
458
|
+
return previewKeywords.contains { className.contains($0) }
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
private static func findCompletionButton(in rootViewController: UIViewController) -> UIBarButtonItem? {
|
|
462
|
+
var controllersToVisit: [UIViewController] = [rootViewController]
|
|
463
|
+
var visitedControllers = Set<ObjectIdentifier>()
|
|
464
|
+
var fallbackButton: UIBarButtonItem?
|
|
465
|
+
|
|
466
|
+
while !controllersToVisit.isEmpty {
|
|
467
|
+
let currentController = controllersToVisit.removeFirst()
|
|
468
|
+
let identifier = ObjectIdentifier(currentController)
|
|
469
|
+
|
|
470
|
+
guard visitedControllers.insert(identifier).inserted else {
|
|
471
|
+
continue
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
let rightBarButtonItems = currentController.navigationItem.rightBarButtonItems ?? []
|
|
475
|
+
for barButtonItem in rightBarButtonItems where barButtonItem.isEnabled && barButtonItem.action != nil {
|
|
476
|
+
if isCompletionButton(barButtonItem) {
|
|
477
|
+
return barButtonItem
|
|
478
|
+
}
|
|
479
|
+
fallbackButton = fallbackButton ?? barButtonItem
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
if let rightBarButtonItem = currentController.navigationItem.rightBarButtonItem,
|
|
483
|
+
rightBarButtonItem.isEnabled,
|
|
484
|
+
rightBarButtonItem.action != nil {
|
|
485
|
+
if isCompletionButton(rightBarButtonItem) {
|
|
486
|
+
return rightBarButtonItem
|
|
487
|
+
}
|
|
488
|
+
fallbackButton = fallbackButton ?? rightBarButtonItem
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if let navigationController = currentController as? UINavigationController {
|
|
492
|
+
controllersToVisit.append(contentsOf: navigationController.viewControllers)
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
controllersToVisit.append(contentsOf: currentController.children)
|
|
496
|
+
|
|
497
|
+
if let parentController = currentController.parent {
|
|
498
|
+
controllersToVisit.append(parentController)
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if let presentedViewController = currentController.presentedViewController {
|
|
502
|
+
controllersToVisit.append(presentedViewController)
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return fallbackButton
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
private static func isCompletionButton(_ barButtonItem: UIBarButtonItem) -> Bool {
|
|
510
|
+
if barButtonItem.style == .done {
|
|
511
|
+
return true
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
let normalizedTitle = normalizeActionText(barButtonItem.title)
|
|
515
|
+
return normalizedTitle == "done" || normalizedTitle == "save" || normalizedTitle == "done scanning"
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
private static func normalizeActionText(_ text: String?) -> String {
|
|
519
|
+
text?
|
|
520
|
+
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
521
|
+
.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current)
|
|
522
|
+
.lowercased() ?? ""
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
private static func previewHotspotFrame(in rootView: UIView) -> CGRect {
|
|
526
|
+
CGRect(
|
|
527
|
+
x: 0,
|
|
528
|
+
y: rootView.bounds.height * 0.56,
|
|
529
|
+
width: rootView.bounds.width * 0.42,
|
|
530
|
+
height: rootView.bounds.height * 0.44
|
|
531
|
+
)
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
private static func suppressLimitModalIfNeeded() {
|
|
535
|
+
let generation = documentScanModalSuppressionGeneration + 1
|
|
536
|
+
documentScanModalSuppressionGeneration = generation
|
|
537
|
+
suppressLimitModalIfNeeded(generation: generation, remainingAttempts: 12)
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
private static func suppressLimitModalIfNeeded(generation: Int, remainingAttempts: Int) {
|
|
541
|
+
DispatchQueue.main.async {
|
|
542
|
+
guard generation == documentScanModalSuppressionGeneration else {
|
|
543
|
+
return
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
dismissVisibleLimitModalIfNeeded()
|
|
547
|
+
|
|
548
|
+
guard remainingAttempts > 0 else {
|
|
549
|
+
return
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
|
|
553
|
+
suppressLimitModalIfNeeded(generation: generation, remainingAttempts: remainingAttempts - 1)
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
private static func dismissVisibleLimitModalIfNeeded() {
|
|
559
|
+
guard let documentCameraViewController = activeDocumentCameraController.value else {
|
|
560
|
+
return
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
let modalsToDismiss = findPresentedModals(around: documentCameraViewController)
|
|
564
|
+
for modal in modalsToDismiss {
|
|
565
|
+
modal.view.alpha = 0
|
|
566
|
+
modal.view.isHidden = true
|
|
567
|
+
modal.dismiss(animated: false)
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
private static func findPresentedModals(around rootViewController: UIViewController) -> [UIViewController] {
|
|
572
|
+
var results: [UIViewController] = []
|
|
573
|
+
var controllersToVisit: [UIViewController] = [rootViewController]
|
|
574
|
+
var visitedControllers = Set<ObjectIdentifier>()
|
|
575
|
+
var seenResults = Set<ObjectIdentifier>()
|
|
576
|
+
|
|
577
|
+
while !controllersToVisit.isEmpty {
|
|
578
|
+
let currentController = controllersToVisit.removeFirst()
|
|
579
|
+
let identifier = ObjectIdentifier(currentController)
|
|
580
|
+
|
|
581
|
+
guard visitedControllers.insert(identifier).inserted else {
|
|
582
|
+
continue
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
if let presentedViewController = currentController.presentedViewController,
|
|
586
|
+
shouldSuppressModal(presentedViewController, scannerRoot: rootViewController),
|
|
587
|
+
seenResults.insert(ObjectIdentifier(presentedViewController)).inserted {
|
|
588
|
+
results.append(presentedViewController)
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
if let navigationController = currentController as? UINavigationController {
|
|
592
|
+
controllersToVisit.append(contentsOf: navigationController.viewControllers)
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
controllersToVisit.append(contentsOf: currentController.children)
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
if let windowScene = rootViewController.view.window?.windowScene {
|
|
599
|
+
for window in windowScene.windows {
|
|
600
|
+
guard let windowRootViewController = window.rootViewController else {
|
|
601
|
+
continue
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
if let modal = topPresentedViewController(from: windowRootViewController),
|
|
605
|
+
shouldSuppressModal(modal, scannerRoot: rootViewController),
|
|
606
|
+
seenResults.insert(ObjectIdentifier(modal)).inserted {
|
|
607
|
+
results.append(modal)
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
return results
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
private static func topPresentedViewController(from rootViewController: UIViewController) -> UIViewController? {
|
|
616
|
+
var currentViewController: UIViewController? = rootViewController
|
|
617
|
+
var lastPresentedViewController: UIViewController?
|
|
618
|
+
|
|
619
|
+
while let presentedViewController = currentViewController?.presentedViewController {
|
|
620
|
+
lastPresentedViewController = presentedViewController
|
|
621
|
+
currentViewController = presentedViewController
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
return lastPresentedViewController
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
private static func shouldSuppressModal(_ viewController: UIViewController, scannerRoot: UIViewController) -> Bool {
|
|
628
|
+
guard viewController !== scannerRoot else {
|
|
629
|
+
return false
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
if viewController is UIAlertController {
|
|
633
|
+
return true
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
let className = NSStringFromClass(type(of: viewController)).lowercased()
|
|
637
|
+
return className.contains("alert") || className.contains("sheet") || className.contains("prompt")
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
static func attach(controller: VNDocumentCameraViewController) {
|
|
641
|
+
activeDocumentCameraController.value = controller
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
#else
|
|
647
|
+
|
|
648
|
+
enum VisionKitScanSwizzle {
|
|
649
|
+
static let isCompiledIn = false
|
|
650
|
+
|
|
651
|
+
static func prepare(limit: Int?, presentPreviewAfterCapture: Bool) {
|
|
652
|
+
guard limit != nil || presentPreviewAfterCapture else {
|
|
653
|
+
return
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
guard !didLogDisabledSwizzle else {
|
|
657
|
+
return
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
didLogDisabledSwizzle = true
|
|
661
|
+
NSLog(
|
|
662
|
+
"[DocumentScanner] Private VisionKit page-limit swizzle is not compiled in. " +
|
|
663
|
+
"maxNumDocuments only trims pages after Save. " +
|
|
664
|
+
"Set plugins.DocumentScanner.iosVisionKitSwizzle to true and run cap sync to compile it."
|
|
665
|
+
)
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
static func reset() {}
|
|
669
|
+
|
|
670
|
+
static func attach(controller: VNDocumentCameraViewController) {}
|
|
671
|
+
|
|
672
|
+
private static var didLogDisabledSwizzle = false
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
#endif
|
|
@@ -7,7 +7,9 @@ final class DocumentScannerTests: XCTestCase {
|
|
|
7
7
|
XCTAssertEqual(ResponseType.imageFilePath, "imageFilePath")
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
func
|
|
10
|
+
func testVisionKitSwizzleMatchesCompileFlag() {
|
|
11
|
+
#if CAPGO_DOCUMENT_SCANNER_VISIONKIT_SWIZZLE
|
|
12
|
+
XCTAssertTrue(VisionKitScanSwizzle.isCompiledIn)
|
|
11
13
|
XCTAssertEqual(
|
|
12
14
|
VisionKitPrivateConstants.inProcessViewControllerClassName,
|
|
13
15
|
"VNDocumentCameraViewController_InProcess"
|
|
@@ -16,5 +18,8 @@ final class DocumentScannerTests: XCTestCase {
|
|
|
16
18
|
VisionKitPrivateConstants.documentCameraCanAddImagesSelector,
|
|
17
19
|
Selector("documentCameraController:canAddImages:")
|
|
18
20
|
)
|
|
21
|
+
#else
|
|
22
|
+
XCTAssertFalse(VisionKitScanSwizzle.isCompiledIn)
|
|
23
|
+
#endif
|
|
19
24
|
}
|
|
20
25
|
}
|