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