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.
startMonitoring(): Checks camera permissions and prepares theDataScannerViewController. Sets status to.readyif successful.getScannerView(): Returns theDataScannerViewControllerinstance, which should be presented by the application (e.g., via aUIViewControllerRepresentable).triggerScan(): Called (usually after the view is presented) to start theDataScannerViewController‘s actual scanning process. Status becomes.scanning.- Barcode data is reported via the
BarcodeScannerDelegate. 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
recognizedDataTypesThe types of data the scanner should look for (e.g.,
[.barcode()]). Defaults to[.barcode()].recognizesMultipleItemsWhether the scanner should detect multiple items in a single session or stop after the first. Defaults to
false. -
startMonitoring()AsynchronousPrepares 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
AScannerErrorif 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
DataScannerViewControllerand de-initializing it. The status is typically set to.readyif 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()AsynchronousStarts 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 theDataScannerViewControlleris initialized, and then callsstartScanning()on it. Status transitions to.scanningon success.Throws
AScannerErrorif permissions are denied, camera is unsupported, initialization fails, orDataScannerViewController.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
DataScannerViewControllerinstance 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
-
Called by
DataScannerViewControllerwhen new items (barcodes, text) are recognized.Declaration
Swift
@MainActor public func dataScanner(_ dataScanner: DataScannerViewController, didAdd addedItems: [RecognizedItem], allItems: [RecognizedItem]) -
Called by
DataScannerViewControllerwhen 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
DataScannerViewControllerwhen recognized items are updated (e.g., refined position).Declaration
Swift
@MainActor public func dataScanner(_ dataScanner: DataScannerViewController, didUpdate updatedItems: [RecognizedItem], allItems: [RecognizedItem]) -
Called by
DataScannerViewControllerif 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., afterstartScanning()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)
-
checkPermissionsAndPrepareForPresentation(manager:Asynchronous) Checks camera permissions and prepares the scanner for presentation.
Declaration
Swift
@MainActor public func checkPermissionsAndPrepareForPresentation(manager: BarcodeScannerManager) async -> Result<Void, ScannerError>Parameters
managerThe
BarcodeScannerManagermanaging this scanner.Return Value
A
Resultindicating success (scanner is ready to present) or failure (with aScannerError).