VisionKitScannerRepresentable

@MainActor
public struct VisionKitScannerRepresentable : UIViewControllerRepresentable

A SwiftUI UIViewControllerRepresentable that wraps a VisionKitScanner to display a VisionKit barcode scanner.

VisionKitScannerRepresentable integrates with BarcodeScannerManager to present a DataScannerViewController for barcode scanning on iOS devices (excluding Mac Catalyst). It handles the scanner’s lifecycle, including permission checks, torch state synchronization, and control button visibility (cancel and flash buttons). If the scanner is unavailable (e.g., on Mac Catalyst or due to permission issues), it displays a placeholder view with a “Scanner Unavailable” message.

Note

This view requires VisionKit and is only functional on iOS (not Mac Catalyst or visionOS). The scannerManager must have an active VisionKitScanner instance, typically activated via scannerManager.setActiveScanner(.visionKit).

Usage

Initialize with a BarcodeScannerManager instance, a binding for torch state, and a cancel callback:

@State private var isTorchOn = false
@StateObject private var scannerManager = BarcodeScannerManager(
    recognizedDataTypes: [.barcode(symbologies: [.qr, .ean13])],
    recognizesMultipleItems: false
)

var body: some View {
    VisionKitScannerRepresentable(
        scannerManager: scannerManager,
        isTorchOn: $isTorchOn,
        onCancelTapped: {
            scannerManager.stopMonitoring()
        },
        showCancelButton: true,
        showFlashButton: true
    )
    .ignoresSafeArea()
}
  • Ensure scannerManager is configured with BarcodeDataType for barcode types.
  • The isTorchOn binding syncs with the scanner’s torch state.
  • The onCancelTapped closure is called when the cancel button (if shown) is tapped.

Requirements

  • iOS 15.0+.
  • Vision framework for VNBarcodeSymbology if specifying barcode symbologies.
  • BarcodeScannerManager and VisionKitScanner.
  • Camera permissions must be granted for scanning to work.
  • The BarcodeScannerManager instance managing the active VisionKitScanner.

    Declaration

    Swift

    @ObservedObject
    @MainActor
    public var scannerManager: BarcodeScannerManager { get set }
  • A binding to control and reflect the scanner’s torch state.

    Declaration

    Swift

    @Binding
    @MainActor
    public var isTorchOn: Bool { get nonmutating set }
  • A closure called when the cancel button (if shown) is tapped.

    Declaration

    Swift

    @MainActor
    public var onCancelTapped: () -> Void
  • A Boolean indicating whether to show the cancel button in the scanner UI.

    Declaration

    Swift

    @MainActor
    public var showCancelButton: Bool
  • A Boolean indicating whether to show the flash (torch) button in the scanner UI.

    Declaration

    Swift

    @MainActor
    public var showFlashButton: Bool
  • Initializes the VisionKitScannerRepresentable with a BarcodeScannerManager and configuration.

    Declaration

    Swift

    @MainActor
    public init(
        scannerManager: BarcodeScannerManager,
        isTorchOn: Binding<Bool>,
        onCancelTapped: @escaping () -> Void,
        showCancelButton: Bool = true,
        showFlashButton: Bool = true
    )

    Parameters

    scannerManager

    The BarcodeScannerManager instance managing the VisionKitScanner.

    isTorchOn

    A binding to control and observe the torch state.

    onCancelTapped

    A closure called when the cancel button is tapped.

    showCancelButton

    Whether to display the cancel button. Defaults to true.

    showFlashButton

    Whether to display the flash button (if torch is available). Defaults to true.

  • The type of UIViewController managed by this representable.

    Declaration

    Swift

    public typealias UIViewControllerType = UIViewController
  • Creates the UIViewController for the scanner or a placeholder if unavailable.

    This method retrieves the VisionKitScanner from the scannerManager, checks permissions, and returns the scanner’s UIViewController (typically a DataScannerViewController). If the scanner is unavailable (e.g., on Mac Catalyst or due to missing permissions), it returns a placeholder UIViewController with a “Scanner Unavailable” message.

    Declaration

    Swift

    @MainActor
    public func makeUIViewController(context: Context) -> UIViewController

    Parameters

    context

    The context provided by SwiftUI.

    Return Value

    A UIViewController containing the scanner UI or a placeholder.

  • Updates the UIViewController with the latest configuration.

    This method syncs the scanner’s properties (showCancelButton, showFlashButton, onCancelTapped, and isTorchOn) with the representable’s state. If the scanner is not active or unavailable, it attempts to activate it. If the torch state changes, it calls toggleTorch() to update the hardware state and syncs the binding.

    Declaration

    Swift

    @MainActor
    public func updateUIViewController(_ uiViewController: UIViewController, context: Context)

    Parameters

    uiViewController

    The UIViewController to update.

    context

    The context provided by SwiftUI.