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 requiresVisionKit 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
scannerManageris configured withBarcodeDataTypefor barcode types. - The
isTorchOnbinding syncs with the scanner’s torch state. - The
onCancelTappedclosure is called when the cancel button (if shown) is tapped.
Requirements
- iOS 15.0+.
Visionframework forVNBarcodeSymbologyif specifying barcode symbologies.BarcodeScannerManagerandVisionKitScanner.- Camera permissions must be granted for scanning to work.
-
The
BarcodeScannerManagerinstance managing the activeVisionKitScanner.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
VisionKitScannerRepresentablewith aBarcodeScannerManagerand configuration.Declaration
Swift
@MainActor public init( scannerManager: BarcodeScannerManager, isTorchOn: Binding<Bool>, onCancelTapped: @escaping () -> Void, showCancelButton: Bool = true, showFlashButton: Bool = true )Parameters
scannerManagerThe
BarcodeScannerManagerinstance managing theVisionKitScanner.isTorchOnA binding to control and observe the torch state.
onCancelTappedA closure called when the cancel button is tapped.
showCancelButtonWhether to display the cancel button. Defaults to
true.showFlashButtonWhether to display the flash button (if torch is available). Defaults to
true. -
The type of
UIViewControllermanaged by this representable.Declaration
Swift
public typealias UIViewControllerType = UIViewController -
Creates the
UIViewControllerfor the scanner or a placeholder if unavailable.This method retrieves the
VisionKitScannerfrom thescannerManager, checks permissions, and returns the scanner’sUIViewController(typically aDataScannerViewController). If the scanner is unavailable (e.g., on Mac Catalyst or due to missing permissions), it returns a placeholderUIViewControllerwith a “Scanner Unavailable” message.Declaration
Swift
@MainActor public func makeUIViewController(context: Context) -> UIViewControllerParameters
contextThe context provided by SwiftUI.
Return Value
A
UIViewControllercontaining the scanner UI or a placeholder. -
Updates the
UIViewControllerwith the latest configuration.This method syncs the scanner’s properties (
showCancelButton,showFlashButton,onCancelTapped, andisTorchOn) with the representable’s state. If the scanner is not active or unavailable, it attempts to activate it. If the torch state changes, it callstoggleTorch()to update the hardware state and syncs the binding.Declaration
Swift
@MainActor public func updateUIViewController(_ uiViewController: UIViewController, context: Context)Parameters
uiViewControllerThe
UIViewControllerto update.contextThe context provided by SwiftUI.