From 7cf369b5aad559e312bb08f5abd823dc6e5ccdd6 Mon Sep 17 00:00:00 2001 From: Bryan Gonzales Date: Thu, 28 Oct 2021 15:46:17 -0500 Subject: [PATCH 1/2] Continue scanning after detecting other object type --- .../BarcodeScannerViewController.swift | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/Sources/Controllers/BarcodeScannerViewController.swift b/Sources/Controllers/BarcodeScannerViewController.swift index 5f86bcc..061126e 100644 --- a/Sources/Controllers/BarcodeScannerViewController.swift +++ b/Sources/Controllers/BarcodeScannerViewController.swift @@ -14,7 +14,7 @@ public protocol BarcodeScannerCodeDelegate: class { /// Delegate to report errors. public protocol BarcodeScannerErrorDelegate: class { - func scanner(_ controller: BarcodeScannerViewController, didReceiveError error: Error) + func scanner(_ controller: BarcodeScannerViewController, didReceiveError error: BarcodeScannerError) } /// Delegate to dismiss barcode scanner when the close button has been pressed. @@ -22,6 +22,17 @@ public protocol BarcodeScannerDismissalDelegate: class { func scannerDidDismiss(_ controller: BarcodeScannerViewController) } +// MARK: - Error types + +public enum BarcodeScannerError: Error { + /// Error when something besides a MachineReadableCodeObject was detected. (Check AVMetadataObject.ObjectType documentation) + case nonMachineReadableCodeDetected + /// Error describing an unexpected/general error + case unexpected(Error) + /// Error when a MachineReadableCodeObject was detected but its metadata is unsupported + case unsupported +} + // MARK: - Controller /** @@ -45,6 +56,9 @@ open class BarcodeScannerViewController: UIViewController { /// Delegate to dismiss barcode scanner when the close button has been pressed. public weak var dismissalDelegate: BarcodeScannerDismissalDelegate? + /// Stop scanning when other object besides a MachineReadableCodeObject is detected + public var stopCaptureWhenDetectingOtherObject = true + /// `AVCaptureMetadataOutput` metadata object types. public var metadata = AVMetadataObject.ObjectType.barcodeScannerMetadata { didSet { @@ -236,7 +250,7 @@ extension BarcodeScannerViewController: CameraViewControllerDelegate { } func cameraViewController(_ controller: CameraViewController, didReceiveError error: Error) { - errorDelegate?.scanner(self, didReceiveError: error) + errorDelegate?.scanner(self, didReceiveError: .unexpected(error)) } func cameraViewControllerDidTapSettingsButton(_ controller: CameraViewController) { @@ -251,15 +265,17 @@ extension BarcodeScannerViewController: CameraViewControllerDelegate { didOutput metadataObjects: [AVMetadataObject]) { guard !locked && isVisible else { return } guard !metadataObjects.isEmpty else { return } + guard let metadataObj = metadataObjects.first as? AVMetadataMachineReadableCodeObject else { + if self.stopCaptureWhenDetectingOtherObject { controller.stopCapturing() } + errorDelegate?.scanner(self, didReceiveError: .nonMachineReadableCodeDetected) + return + } + controller.stopCapturing() - - guard - let metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObject, - var code = metadataObj.stringValue, - metadata.contains(metadataObj.type) - else { - errorDelegate?.scanner(self, didReceiveError: NSError()) - return + + guard var code = metadataObj.stringValue, metadata.contains(metadataObj.type) else { + errorDelegate?.scanner(self, didReceiveError: .unsupported) + return } var rawType = metadataObj.type.rawValue From 185536293b0de1726ddd604ba32ae2c955486889 Mon Sep 17 00:00:00 2001 From: Bryan Gonzales Date: Thu, 28 Oct 2021 15:47:23 -0500 Subject: [PATCH 2/2] Update podspec to 5.0.5 --- BarcodeScanner.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BarcodeScanner.podspec b/BarcodeScanner.podspec index 6a80de5..9240eea 100644 --- a/BarcodeScanner.podspec +++ b/BarcodeScanner.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "BarcodeScanner" s.summary = "Simple and beautiful barcode scanner." - s.version = "5.0.4" + s.version = "5.0.5" s.homepage = "https://github.com/hyperoslo/BarcodeScanner" s.license = 'MIT' s.author = { "Hyper Interaktiv AS" => "ios@hyper.no" }