VisionKitScanner

@MainActor
public final class VisionKitScanner : NSObject, BarcodeScanner
extension VisionKitScanner: DataScannerViewControllerDelegate

A barcode scanner implementation using Apple’s VisionKit framework.

This class utilizes the DataScannerViewController to scan barcodes and text using the device’s camera. It conforms to the BarcodeScanner protocol.

Usage

The BarcodeScannerManager typically manages instances of this scanner.

  1. startMonitoring(): Checks camera permissions and prepares the DataScannerViewController. Sets status to .ready if successful.
  2. getScannerView(): Returns the DataScannerViewController instance, which should be presented by the application (e.g., via a UIViewControllerRepresentable).
  3. triggerScan(): Called (usually after the view is presented) to start the DataScannerViewController‘s actual scanning process. Status becomes .scanning.
  4. Barcode data is reported via the BarcodeScannerDelegate.
  5. stopMonitoring(): Stops the scan and releases the camera resources.

Ensure the NSCameraUsageDescription key is included in your app’s Info.plist.

  • The type of this scanner, .visionKit.

    Declaration

    Swift

    @MainActor
    public let type: ScannerType
  • The delegate responsible for receiving status updates and scanned barcode data.

    Declaration

    Swift

    @MainActor
    public weak var delegate: (any BarcodeScannerDelegate)?
  • The current status of the VisionKit scanner. Changes to this property are logged and reported to the delegate.

    Declaration

    Swift

    @MainActor
    public private(set) var currentStatus: ScannerStatus { get set }
  • Undocumented

    Declaration

    Swift

    @MainActor
    public var visionScannerVC: DataScannerViewController?
  • A Boolean value indicating whether the scanner should continue scanning after finding an item, or stop after the first successful scan. This is passed to the DataScannerViewController.

    Declaration

    Swift

    @MainActor
    public let recognizesMultipleItems: Bool
  • Whether to show the Cancel button in the scanning view. Defaults to true.

    Declaration

    Swift

    @MainActor
    public var showCancelButton: Bool
  • Whether to show the flash button in the scanning view. Defaults to true.

    Declaration

    Swift

    @MainActor
    public var showFlashButton: Bool
  • Closure called when the Cancel button is tapped.

    Declaration

    Swift

    @MainActor
    public var onCancelTapped: (() -> Void)?
  • Tracks the torch state internally.

    Declaration

    Swift

    @MainActor
    public var isTorchOn: Bool
  • Initializes a new VisionKitScanner.

    Declaration

    Swift

    @MainActor
    public init(
        recognizedDataTypes: Set<BarcodeDataType> = [.barcode()],
        recognizesMultipleItems: Bool = false
    )

    Parameters

    recognizedDataTypes

    The types of data the scanner should look for (e.g., [.barcode()]). Defaults to [.barcode()].

    recognizesMultipleItems

    Whether the scanner should detect multiple items in a single session or stop after the first. Defaults to false.

  • startMonitoring() Asynchronous

    Prepares the VisionKit scanner for operation.

    This method checks for camera support and permissions. If granted, it initializes the DataScannerViewController (if not already done) and sets the status to .ready. The actual camera view is not started by this method; triggerScan() does that.

    Throws

    A ScannerError if camera is not supported or permissions are denied.

    Declaration

    Swift

    @MainActor
    public func startMonitoring() async throws
  • Stops the VisionKit scanner and releases associated resources.

    This involves stopping any active scan on the DataScannerViewController and de-initializing it. The status is typically set to .ready if permissions are still valid, allowing for a restart, or an appropriate error state if permissions were revoked or the camera became unsupported.

    Declaration

    Swift

    @MainActor
    public func stopMonitoring()
  • triggerScan() Asynchronous

    Starts the actual scanning process using the DataScannerViewController.

    This method should be called when the scanner’s view (obtained via getScannerView()) is visible. It checks permissions and support again, ensures the DataScannerViewController is initialized, and then calls startScanning() on it. Status transitions to .scanning on success.

    Throws

    A ScannerError if permissions are denied, camera is unsupported, initialization fails, or DataScannerViewController.startScanning() fails.

    Declaration

    Swift

    @MainActor
    public func triggerScan() async throws
  • Resets the VisionKit scanner to its initial state.

    Declaration

    Swift

    @MainActor
    public func reset()
  • Returns the DataScannerViewController instance to be presented for camera scanning.

    Declaration

    Swift

    @MainActor
    public func getScannerView() -> UIViewController?
  • Checks if the VisionKit scanner is available on this device.

    Declaration

    Swift

    @MainActor
    public func isAvailable() -> Bool
  • Undocumented

    Declaration

    Swift

    @MainActor
    public func toggleTorch() -> Bool
  • Undocumented

    Declaration

    Swift

    @MainActor
    public var isTorchAvailable: Bool { get }
  • Undocumented

    Declaration

    Swift

    @MainActor
    public func makeControlButtonsView(isTorchOn: Binding<Bool>, onCancelTapped: @escaping () -> Void) -> some View

DataScannerViewControllerDelegate Callbacks

  • Called by DataScannerViewController when new items (barcodes, text) are recognized.

    Declaration

    Swift

    @MainActor
    public func dataScanner(_ dataScanner: DataScannerViewController, didAdd addedItems: [RecognizedItem], allItems: [RecognizedItem])
  • Called by DataScannerViewController when items are removed from recognition (e.g., moved out of view).

    Declaration

    Swift

    @MainActor
    public func dataScanner(_ dataScanner: DataScannerViewController, didRemove removedItems: [RecognizedItem], allItems: [RecognizedItem])
  • Called by DataScannerViewController when recognized items are updated (e.g., refined position).

    Declaration

    Swift

    @MainActor
    public func dataScanner(_ dataScanner: DataScannerViewController, didUpdate updatedItems: [RecognizedItem], allItems: [RecognizedItem])
  • Called by DataScannerViewController if it becomes unavailable due to an error (e.g., system interruption).

    Declaration

    Swift

    @MainActor
    public func dataScanner(_ dataScanner: DataScannerViewController, becameUnavailableWithError error: Error)
  • Called when the DataScannerViewController‘s view becomes active (e.g., after startScanning() is successful).

    Declaration

    Swift

    @MainActor
    public func dataScannerDidBecomeActive(_ dataScanner: DataScannerViewController)
  • Called when the DataScannerViewController‘s view disappears or becomes inactive.

    Declaration

    Swift

    @MainActor
    public func dataScannerViewDidDisappear(_ dataScanner: DataScannerViewController)

Private Helper Methods