diff --git a/Example/BarcodeScannerExample/Podfile.lock b/Example/BarcodeScannerExample/Podfile.lock index 4a3d3af..73c751d 100644 --- a/Example/BarcodeScannerExample/Podfile.lock +++ b/Example/BarcodeScannerExample/Podfile.lock @@ -9,8 +9,8 @@ EXTERNAL SOURCES: :path: ../../ SPEC CHECKSUMS: - BarcodeScanner: 91ac9634d249b4e59a5b57dff9264b691bc99ea6 + BarcodeScanner: e4b51b1c1c398bbf649e02da0d62c8c7a7de4c4f PODFILE CHECKSUM: ea40d735f047f0ae7ae319d7a320a82facf3361f -COCOAPODS: 1.3.1 +COCOAPODS: 1.4.0 diff --git a/Sources/Controllers/BarcodeScannerViewController.swift b/Sources/Controllers/BarcodeScannerViewController.swift index eb835dc..417a4e3 100644 --- a/Sources/Controllers/BarcodeScannerViewController.swift +++ b/Sources/Controllers/BarcodeScannerViewController.swift @@ -60,6 +60,8 @@ open class BarcodeScannerViewController: UIViewController { private var locked = false /// Flag to check if layout constraints has been activated. private var constraintsActivated = false + /// Flag to check if view controller is currently on screen + private var isVisible = false // MARK: - UI @@ -106,6 +108,12 @@ open class BarcodeScannerViewController: UIViewController { open override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) setupCameraConstraints() + isVisible = true + } + + open override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + isVisible = false } // MARK: - State handling @@ -274,7 +282,6 @@ private extension BarcodeScannerViewController { extension BarcodeScannerViewController: HeaderViewControllerDelegate { func headerViewControllerDidTapCloseButton(_ controller: HeaderViewController) { - status = Status(state: .scanning) dismissalDelegate?.scannerDidDismiss(self) } } @@ -304,7 +311,7 @@ extension BarcodeScannerViewController: CameraViewControllerDelegate { func cameraViewController(_ controller: CameraViewController, didOutput metadataObjects: [AVMetadataObject]) { - guard !locked else { return } + guard !locked && isVisible else { return } guard !metadataObjects.isEmpty else { return } guard diff --git a/Sources/Controllers/CameraViewController.swift b/Sources/Controllers/CameraViewController.swift index b184d2e..c7bea10 100644 --- a/Sources/Controllers/CameraViewController.swift +++ b/Sources/Controllers/CameraViewController.swift @@ -81,6 +81,7 @@ public final class CameraViewController: UIViewController { // MARK: - Initialization deinit { + stopCapturing() NotificationCenter.default.removeObserver(self) } @@ -112,11 +113,6 @@ public final class CameraViewController: UIViewController { animateFocusView() } - public override func viewWillDisappear(_ animated: Bool) { - super.viewWillDisappear(animated) - captureSession.stopRunning() - } - public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { super.viewWillTransition(to: size, with: coordinator)