BarcodeDataType
public enum BarcodeDataType : Hashable
extension BarcodeDataType: CustomStringConvertible
Manages various barcode scanner implementations, providing a unified interface for applications.
BarcodeScannerManager
is an ObservableObject
that allows SwiftUI views to react to changes
in scanner status, received barcodes, and pairing QR codes. It acts as a facade, delegating
operations to the currently active scanner instance (e.g., VisionKitScanner
, ProGloveScanner
).
Overview
The manager holds instances of all supported scanner types. An application can select an active
scanner type using setActiveScanner(_:)
. Once a scanner is active, the manager proxies
calls like startMonitoring()
, triggerScan()
, getPairingQRCode()
, etc., to that instance.
It also acts as a delegate (BarcodeScannerDelegate
) for the active scanner, receiving status updates
and barcode data, which it then publishes for the application to consume via its @Published
properties
or callback closures (onStatusChanged
, onBarcodeScanned
).
Usage
Initialization
Typically, you would use the shared singleton instance:
@StateObject private var scannerManager = BarcodeScannerManager.shared
Or, if you need custom configurations (e.g., for VisionKit):
@StateObject private var scannerManager = BarcodeScannerManager(
recognizedDataTypes: [.barcode(symbologies: [.qr, .ean13])], // Specify symbologies for VisionKit
recognizesMultipleItems: false, // For VisionKit: stop after first scan
serviceUUID: "YOUR_CUSTOM_IPC_SERVICE_UUID" // Optional: For IPCMobile custom service
)
Setting Callbacks
Assign closures to onStatusChanged
and onBarcodeScanned
to receive updates:
.onAppear {
scannerManager.onStatusChanged = { status in
// Handle status change (e.g., update UI, show errors)
if case .error(let error) = status {
self.errorMessage = error.localizedDescription
}
}
scannerManager.onBarcodeScanned = { barcodeValue in
// Handle received barcode
self.scannedValue = barcodeValue
// If using VisionKit and it's a single scan, you might want to dismiss its view.
}
}
Selecting and Activating a Scanner
Before using a scanner, it must be activated:
func activateMyScanner(type: ScannerType) async {
do {
try await scannerManager.setActiveScanner(type)
// Scanner is now active and its startMonitoring() has been called.
// Status should update to .idle, .ready, or an error.
} catch {
// Handle activation error (e.g., scanner.startMonitoring() failed)
self.errorMessage = (error as? ScannerError)?.message ?? error.localizedDescription
}
}
A platform-agnostic enum to represent data types for scanners, replacing VisionKit’s RecognizedDataType.
-
Undocumented
Declaration
Swift
case barcode(symbologies: [VNBarcodeSymbology] = [])
-
Undocumented
Declaration
Swift
case text
-
Declaration
Swift
public var description: String { get }