diff --git a/Documentation Code Snippets/Barcode RTUUIV2/AR Overlay/AROverlayBarcodeScannerUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/AR Overlay/AROverlayBarcodeScannerUI2ViewController.swift index b59ee47..11c9417 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/AR Overlay/AROverlayBarcodeScannerUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/AR Overlay/AROverlayBarcodeScannerUI2ViewController.swift @@ -14,10 +14,12 @@ class AROverlayBarcodeScannerUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -36,16 +38,29 @@ class AROverlayBarcodeScannerUI2ViewController: UIViewController { configuration.useCase = usecase // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/ActionBar/ActionBarConfigurationUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/ActionBar/ActionBarConfigurationUI2ViewController.swift index 2a07247..1656eab 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/ActionBar/ActionBarConfigurationUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/ActionBar/ActionBarConfigurationUI2ViewController.swift @@ -14,10 +14,12 @@ class ActionBarConfigurationUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -51,16 +53,29 @@ class ActionBarConfigurationUI2ViewController: UIViewController { actionBar.flipCameraButton.foregroundColor = SBSDKUI2Color(colorString: "#FFFFFF") // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/FindAndPick/FindAndPickBarcodeScannerUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/FindAndPick/FindAndPickBarcodeScannerUI2ViewController.swift index 39fe39c..ee9b9a5 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/FindAndPick/FindAndPickBarcodeScannerUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/FindAndPick/FindAndPickBarcodeScannerUI2ViewController.swift @@ -14,10 +14,12 @@ class FindAndPickBarcodeScannerUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -53,13 +55,26 @@ class FindAndPickBarcodeScannerUI2ViewController: UIViewController { configuration.useCase = usecase // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/Getting Started/BarcodeGettingStartedUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/Getting Started/BarcodeGettingStartedUI2ViewController.swift index b3e7fcc..463d139 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/Getting Started/BarcodeGettingStartedUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/Getting Started/BarcodeGettingStartedUI2ViewController.swift @@ -12,38 +12,38 @@ class BarcodeGettingStartedUI2ViewController: UIViewController { func launchRTUUIv2Scanner() { - // Create the default configuration object. - let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() - - // Present the scanner view controller modally on this view controller. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in - - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + Task { + // Create the default configuration object. + let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() - controller.presentingViewController?.dismiss(animated: true) + // Present the view controller modally. + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, + configuration: configuration) + // Process the result as needed. + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") + + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") + } } } func handleScanResults() { - // Create the default configuration object. - let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() - - // Present the scanner view controller modally on this view controller. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in - - if cancelled { - - // Indicates that the cancel button was tapped. - controller.presentingViewController?.dismiss(animated: true) - - } else if let items = result?.items { - + Task { + // Create the default configuration object. + let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() + + // Present the view controller modally. + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, + configuration: configuration) // Process result - items.forEach({ item in + result.items.forEach({ item in print("Barcode Identity String: \(item.barcode.identityString)") print("Scan count: \(item.count)") @@ -78,9 +78,15 @@ class BarcodeGettingStartedUI2ViewController: UIViewController { print("\n" + "\(field.type.displayText ?? ""): \(field.value?.text ?? "")") }) } - }) } + catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") + + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") + } } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/ItemMapper/InfoMappingBarcodeScannerUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/ItemMapper/InfoMappingBarcodeScannerUI2ViewController.swift index d6dff3c..e3bf1db 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/ItemMapper/InfoMappingBarcodeScannerUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/ItemMapper/InfoMappingBarcodeScannerUI2ViewController.swift @@ -14,10 +14,12 @@ class InfoMappingBarcodeScannerUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -59,16 +61,29 @@ class InfoMappingBarcodeScannerUI2ViewController: UIViewController { configuration.useCase = usecase // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/Localization/BarcodeLocalizationUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/Localization/BarcodeLocalizationUI2ViewController.swift index c54ab40..5a8bdcc 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/Localization/BarcodeLocalizationUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/Localization/BarcodeLocalizationUI2ViewController.swift @@ -13,10 +13,12 @@ class BarcodeLocalizationUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -32,13 +34,26 @@ class BarcodeLocalizationUI2ViewController: UIViewController { configuration.localization = localization // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/Multiple Scanning/MultipleBarcodeScannerUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/Multiple Scanning/MultipleBarcodeScannerUI2ViewController.swift index 9d48fe8..7908718 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/Multiple Scanning/MultipleBarcodeScannerUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/Multiple Scanning/MultipleBarcodeScannerUI2ViewController.swift @@ -14,10 +14,12 @@ class MultipleBarcodeScannerUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -25,11 +27,8 @@ class MultipleBarcodeScannerUI2ViewController: UIViewController { // Initialize the multi scan usecase. let multiUsecase = SBSDKUI2MultipleScanningMode() - // Set the counting repeat delay. - multiUsecase.countingRepeatDelay = 1000 - - // Set the counting mode. - multiUsecase.mode = .counting + // Set the unique mode. + multiUsecase.mode = .unique // Set the sheet mode of the barcodes preview. multiUsecase.sheet.mode = .collapsedSheet @@ -48,16 +47,29 @@ class MultipleBarcodeScannerUI2ViewController: UIViewController { configuration.useCase = multiUsecase // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/Palette/BarcodePaletteUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/Palette/BarcodePaletteUI2ViewController.swift index e0b88b0..3738917 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/Palette/BarcodePaletteUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/Palette/BarcodePaletteUI2ViewController.swift @@ -14,10 +14,12 @@ class BarcodePaletteUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -48,16 +50,29 @@ class BarcodePaletteUI2ViewController: UIViewController { configuration.palette = palette // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/PreviewMode/BarcodesSheetModeUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/PreviewMode/BarcodesSheetModeUI2ViewController.swift index a29c69d..a329b35 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/PreviewMode/BarcodesSheetModeUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/PreviewMode/BarcodesSheetModeUI2ViewController.swift @@ -14,10 +14,12 @@ class BarcodesSheetModeUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -39,16 +41,29 @@ class BarcodesSheetModeUI2ViewController: UIViewController { configuration.useCase = multiUsecase // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/Scan And Count/ScanAndCountBarcodeScannerUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/Scan And Count/ScanAndCountBarcodeScannerUI2ViewController.swift new file mode 100644 index 0000000..a7d5e5c --- /dev/null +++ b/Documentation Code Snippets/Barcode RTUUIV2/Scan And Count/ScanAndCountBarcodeScannerUI2ViewController.swift @@ -0,0 +1,78 @@ +// +// ScanAndCountBarcodeScannerUI2ViewController.swift +// ScanbotSDK Examples +// +// Created by Rana Sohaib on 11.11.25. +// + +import Foundation +import ScanbotBarcodeScannerSDK + +class ScanAndCountBarcodeScannerUI2ViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + // Start scanning here. Usually this is an action triggered by some button or menu. + Task { + await self.startScanning() + } + } + + func startScanning() async { + + // Create the default configuration object. + let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() + + // Initialize the multi scan usecase. + let multiUsecase = SBSDKUI2MultipleScanningMode() + + // Set the counting repeat delay. + multiUsecase.countingRepeatDelay = 1000 + + // Set the counting mode. + multiUsecase.mode = .counting + + // Set the sheet mode of the barcodes preview. + multiUsecase.sheet.mode = .collapsedSheet + + // Set the height of the collapsed sheet. + multiUsecase.sheet.collapsedVisibleHeight = .large + + // Enable manual count change. + multiUsecase.sheetContent.manualCountChangeEnabled = true + + // Configure the submit button. + multiUsecase.sheetContent.submitButton.text = "Submit" + multiUsecase.sheetContent.submitButton.foreground.color = SBSDKUI2Color(colorString: "#000000") + + // Set the configured usecase. + configuration.useCase = multiUsecase + + // Create and set an array of accepted barcode formats. + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) + + // Present the view controller modally. + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) + + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") + + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") + } + } +} diff --git a/Documentation Code Snippets/Barcode RTUUIV2/Single Scanning/SingleBarcodeScannerUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/Single Scanning/SingleBarcodeScannerUI2ViewController.swift index ab0b76f..620bd51 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/Single Scanning/SingleBarcodeScannerUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/Single Scanning/SingleBarcodeScannerUI2ViewController.swift @@ -14,10 +14,12 @@ class SingleBarcodeScannerUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -54,16 +56,29 @@ class SingleBarcodeScannerUI2ViewController: UIViewController { configuration.useCase = singleUsecase // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/SwiftUI/BarcodeScannerSwiftUIView.swift b/Documentation Code Snippets/Barcode RTUUIV2/SwiftUI/BarcodeScannerSwiftUIView.swift index 718d807..10a0001 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/SwiftUI/BarcodeScannerSwiftUIView.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/SwiftUI/BarcodeScannerSwiftUIView.swift @@ -10,9 +10,6 @@ import ScanbotBarcodeScannerSDK struct BarcodeScannerSwiftUIView: View { - // A boolean state variable indicating whether the barcode scanner interface should be presented. - @State var showScan: Bool = false - // An instance of `SBSDKUI2BarcodeScannerScreenConfiguration` which contains the configuration settings for the barcode scanner. let configuration: SBSDKUI2BarcodeScannerScreenConfiguration = { @@ -51,7 +48,7 @@ struct BarcodeScannerSwiftUIView: View { configuration.useCase = singleUsecase // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) return configuration }() @@ -63,18 +60,24 @@ struct BarcodeScannerSwiftUIView: View { @State var scannerResult: SBSDKUI2BarcodeScannerUIResult? var body: some View { - if let scannerResult = self.scannerResult { + + if let scannerResult { + // Process and show the results here. + Text("Barcodes scanned: \(scannerResult.items.count)") - } else if let error = self.scanError { + } else if let scanError { + // Show error view here. + Text("Scan error: \(scanError.localizedDescription)") } else { // Show the scanner, pass the configuration and the button and error handlers. SBSDKUI2BarcodeScannerView(configuration: configuration, - onSubmit: { result in scannerResult = result }, - onCancel: { /* Dismiss your view here. */ }, - onError: { error in scanError = error }) + completion: { result, error in + scannerResult = result + scanError = error + }) .ignoresSafeArea() } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/Tiny Barcodes/TinyBarcodeScannerUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/Tiny Barcodes/TinyBarcodeScannerUI2ViewController.swift index c34ba36..10aa502 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/Tiny Barcodes/TinyBarcodeScannerUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/Tiny Barcodes/TinyBarcodeScannerUI2ViewController.swift @@ -14,10 +14,12 @@ class TinyBarcodeScannerUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -25,14 +27,27 @@ class TinyBarcodeScannerUI2ViewController: UIViewController { // Enable locking the focus at the minimum possible distance. configuration.cameraConfiguration.minFocusDistanceLock = true - // Present the scanner view controller modally on this view controller. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + // Present the view controller modally. + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/Top Bar/TopBarBarcodeUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/Top Bar/TopBarBarcodeUI2ViewController.swift index 640960f..391a68e 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/Top Bar/TopBarBarcodeUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/Top Bar/TopBarBarcodeUI2ViewController.swift @@ -14,10 +14,12 @@ class TopBarBarcodeUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -36,16 +38,29 @@ class TopBarBarcodeUI2ViewController: UIViewController { configuration.topBar.cancelButton.foreground.color = SBSDKUI2Color(colorString: "#FFFFFF") // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/User Guidance/BarcodeUserGuidanceUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/User Guidance/BarcodeUserGuidanceUI2ViewController.swift index c931038..c5f56dc 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/User Guidance/BarcodeUserGuidanceUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/User Guidance/BarcodeUserGuidanceUI2ViewController.swift @@ -14,10 +14,12 @@ class BarcodeUserGuidanceUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -36,16 +38,29 @@ class BarcodeUserGuidanceUI2ViewController: UIViewController { userGuidance.background.fillColor = SBSDKUI2Color(colorString: "#7A000000") // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode RTUUIV2/View Finder/BarcodeViewFinderUI2ViewController.swift b/Documentation Code Snippets/Barcode RTUUIV2/View Finder/BarcodeViewFinderUI2ViewController.swift index 0a46d53..10033a5 100644 --- a/Documentation Code Snippets/Barcode RTUUIV2/View Finder/BarcodeViewFinderUI2ViewController.swift +++ b/Documentation Code Snippets/Barcode RTUUIV2/View Finder/BarcodeViewFinderUI2ViewController.swift @@ -14,10 +14,12 @@ class BarcodeViewFinderUI2ViewController: UIViewController { super.viewDidLoad() // Start scanning here. Usually this is an action triggered by some button or menu. - self.startScanning() + Task { + await self.startScanning() + } } - func startScanning() { + func startScanning() async { // Create the default configuration object. let configuration = SBSDKUI2BarcodeScannerScreenConfiguration() @@ -40,16 +42,29 @@ class BarcodeViewFinderUI2ViewController: UIViewController { configuration.viewFinder.overlayColor = SBSDKUI2Color(colorString: "#26000000") // Create and set an array of accepted barcode formats. - configuration.scannerConfiguration.barcodeFormats = SBSDKBarcodeFormats.twod + configuration.scannerConfiguration.setBarcodeFormats(SBSDKBarcodeFormats.twod) // Present the view controller modally. - SBSDKUI2BarcodeScannerViewController.present(on: self, - configuration: configuration) { controller, cancelled, error, result in + do { + let result = try await SBSDKUI2BarcodeScannerViewController.present(on: self, configuration: configuration) - // Completion handler to process the result. - // The `cancelled` parameter indicates if the cancel button was tapped. + // Handle the result. + result.items.forEach { barcodeItem in + // e.g + print(barcodeItem.count) + print(barcodeItem.barcode.format.name) + print(barcodeItem.barcode.text) + print(barcodeItem.barcode.textWithExtension) + // Check out other available properties in `SBSDKBarcodeItem`. + } + print(result.selectedZoomFactor) + + } catch SBSDKError.operationCanceled { + print("The operation was cancelled before completion or by the user") - controller.presentingViewController?.dismiss(animated: true) + } catch { + // Any other error + print("Error scanning barcode: \(error.localizedDescription)") } } } diff --git a/Documentation Code Snippets/Barcode ScanAndCount Scanner/BarcodeScanAndCountViewController.swift b/Documentation Code Snippets/Barcode ScanAndCount Scanner/BarcodeScanAndCountViewController.swift index fc8579e..8916175 100644 --- a/Documentation Code Snippets/Barcode ScanAndCount Scanner/BarcodeScanAndCountViewController.swift +++ b/Documentation Code Snippets/Barcode ScanAndCount Scanner/BarcodeScanAndCountViewController.swift @@ -71,7 +71,7 @@ extension BarcodeScanAndCountViewController: SBSDKBarcodeScanAndCountViewControl for code in codes { // Get the source image. - let sourceImage = code.sourceImage?.toUIImage() + let sourceImage = try? code.sourceImage?.toUIImage() } } @@ -82,4 +82,10 @@ extension BarcodeScanAndCountViewController: SBSDKBarcodeScanAndCountViewControl // Provide a custom overlay view for the barcode return UIImageView(image: UIImage(named: "")) } + + func barcodeScanAndCount(_ controller: SBSDKBarcodeScanAndCountViewController, + didFailScanning error: any Error) { + // Handle the error. + print("Error scanning barcode: \(error.localizedDescription)") + } } diff --git a/Documentation Code Snippets/Barcode and QR-Code Scanner/Classic/BarcodeClassicUIGeneralConfigurationViewController.swift b/Documentation Code Snippets/Barcode and QR-Code Scanner/Classic/BarcodeClassicUIGeneralConfigurationViewController.swift new file mode 100644 index 0000000..368217c --- /dev/null +++ b/Documentation Code Snippets/Barcode and QR-Code Scanner/Classic/BarcodeClassicUIGeneralConfigurationViewController.swift @@ -0,0 +1,122 @@ +// +// BarcodeClassicUIGeneralConfigurationViewController.swift +// ScanbotSDK Examples +// +// Created by Sebastian Husche on 11.11.24. +// + +import Foundation +import ScanbotBarcodeScannerSDK + +class BarcodeClassicUIGeneralConfigurationViewController: UIViewController { + + // The instance of the ClassicUI scanner view controller. + var scannerViewController: SBSDKBarcodeScannerViewController! + + override func viewDidLoad() { + super.viewDidLoad() + + // Create the scanner view controller instance. + self.scannerViewController = SBSDKBarcodeScannerViewController(parentViewController: self, + parentView: self.view, + configuration: .init(), + delegate: self) + + // Now you can configure some general properties using one of the configuration objects. + // + // As demonstrated in the functions below there are 3 steps: + // 1. Read the current configuration from the scanner view controller. + // 2. Modify the configuration to your needs. + // 3. Pass the modified configuration back to the scanner view controller to apply it. + + self.applyGeneralConfiguration() + self.applyZoomConfiguration() + self.applyEnergyConfiguration() + self.applyViewFinderConfiguration() + } + + func applyGeneralConfiguration() { + + // The general configuration lets you control timings, video settings and behaviour etc. + + // Read the current general configuration from the scanner view controller. + let generalConfiguration = self.scannerViewController.generalConfiguration + + // Modify it to your needs. + generalConfiguration.minimumTimeWithoutDeviceMotionBeforeDetection = 0.5 + // To keep session alive until deallocated. + generalConfiguration.cameraSessionKeepAliveTimeout = TimeInterval.greatestFiniteMagnitude + + // After changing the configuration you need to pass it back to the scanner view controller in order to apply it. + self.scannerViewController.generalConfiguration = generalConfiguration + } + + func applyZoomConfiguration() { + + // The zoom configuration lets you control the zooming behaviour of the scanner view controller, e.g. + // if zooming is enabled, the zoom range, the initial zoom factor, discrete zoom steps and zoom related gestures. + + // Read the current zoom configuration from the scanner view controller. + let zoomConfiguration = scannerViewController.zoomConfiguration + + // Modify it to your needs. + zoomConfiguration.isZoomingEnabled = true + zoomConfiguration.zoomRange = SBSDKZoomRange(minZoom: 1.0, maxZoom: 12.0) + zoomConfiguration.initialZoomFactor = 2.0 + zoomConfiguration.isPinchToZoomEnabled = true + + // After changing the configuration you need to pass it back to the scanner view controller in order to apply it. + self.scannerViewController.zoomConfiguration = zoomConfiguration + } + + func applyEnergyConfiguration() { + + // The energy configuration lets you control the energy consumption of the scanner view controller, e.g. by + // turning the energy-safe-mode on or off, changing the detection rates and the inactivity timeout. + + // Read the current energy configuration from the scanner view controller. + let energyConfiguration = scannerViewController.energyConfiguration + + // Modify it to your needs. + energyConfiguration.inactivityTimeout = 10.0 + energyConfiguration.detectionRate = 60 + energyConfiguration.energySaveDetectionRate = 5 + + // After changing the configuration you need to pass it back to the scanner view controller in order to apply it. + self.scannerViewController.energyConfiguration = energyConfiguration + } + + func applyViewFinderConfiguration() { + + // The view finder configuration lets you control the appearance of the view finder, + // e.g. if it is enabled, its aspect ratio, its colors and style, its offsets and insets and more. + + // Read the current view finder configuration from the scanner view controller. + let viewFinderConfiguration = scannerViewController.viewFinderConfiguration + + // Modify it to your needs. + viewFinderConfiguration.isViewFinderEnabled = true + viewFinderConfiguration.aspectRatio = SBSDKAspectRatio(width: 8.0, height: 5.0) + viewFinderConfiguration.lineColor = UIColor.white.withAlphaComponent(0.85) + + // After changing the configuration you need to pass it back to the scanner view controller in order to apply it. + self.scannerViewController.viewFinderConfiguration = viewFinderConfiguration + } + +} + +extension BarcodeClassicUIGeneralConfigurationViewController: SBSDKBarcodeScannerViewControllerDelegate { + + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, + didScanBarcodes codes: [SBSDKBarcodeItem]) { + + // Handle scanned barcodes. + } + + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, + didFailScanning error: any Error) { + // Handle the error. + print("Error scanning barcode: \(error.localizedDescription)") + } +} + diff --git a/Documentation Code Snippets/Barcode and QR-Code Scanner/Classic/BarcodeScannerViewController.swift b/Documentation Code Snippets/Barcode and QR-Code Scanner/Classic/BarcodeScannerViewController.swift index 9402995..99efaf9 100644 --- a/Documentation Code Snippets/Barcode and QR-Code Scanner/Classic/BarcodeScannerViewController.swift +++ b/Documentation Code Snippets/Barcode and QR-Code Scanner/Classic/BarcodeScannerViewController.swift @@ -80,7 +80,7 @@ extension BarcodeScannerViewController: SBSDKBarcodeScannerViewControllerDelegat for code in codes { // Get the source image. - let sourceImage = code.sourceImage?.toUIImage() + let sourceImage = try? code.sourceImage?.toUIImage() } } @@ -88,4 +88,10 @@ extension BarcodeScannerViewController: SBSDKBarcodeScannerViewControllerDelegat func barcodeScannerControllerShouldScanBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool { return self.shouldDetectBarcodes } + + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, + didFailScanning error: any Error) { + // Handle the error. + print("Error scanning barcode: \(error.localizedDescription)") + } } diff --git a/Documentation Code Snippets/Barcode and QR-Code Scanner/DetectionOnImage/BarcodeDetectionOnImage.swift b/Documentation Code Snippets/Barcode and QR-Code Scanner/DetectionOnImage/BarcodeDetectionOnImage.swift index bbf6152..c60b639 100644 --- a/Documentation Code Snippets/Barcode and QR-Code Scanner/DetectionOnImage/BarcodeDetectionOnImage.swift +++ b/Documentation Code Snippets/Barcode and QR-Code Scanner/DetectionOnImage/BarcodeDetectionOnImage.swift @@ -25,16 +25,24 @@ func detectBarcodesOnImage() { // Enable the barcode image extraction. configuration.returnBarcodeImage = true - // Create an instance of `SBSDKBarcodeScanner`, passing the configuration. - let scanner = SBSDKBarcodeScanner(configuration: configuration) - - // Returns the barcode scan result. - let result = scanner.scan(from: image) - - guard let result = result else { return } - - for barcodeItem in result.barcodes { - // Get the source image. - let sourceImage = barcodeItem.sourceImage?.toUIImage() + do { + + // Create an instance of barcode scanner, passing the configuration. + let scanner = try SBSDKBarcodeScanner(configuration: configuration) + + // Create an image ref from UIImage. + let imageRef = SBSDKImageRef.fromUIImage(image: image) + + // Run the scanner on the image. + let result = try scanner.run(image: imageRef) + + for barcodeItem in result.barcodes { + + // Get the source image. + let sourceImage = try barcodeItem.sourceImage?.toUIImage() + } + } + catch { + print("Error detecting barcode: \(error.localizedDescription)") } } diff --git a/Documentation Code Snippets/Barcode and QR-Code Scanner/Handle result/BarcodeHandlingResultViewController.swift b/Documentation Code Snippets/Barcode and QR-Code Scanner/Handle result/BarcodeHandlingResultViewController.swift index dba34a8..556e5a8 100644 --- a/Documentation Code Snippets/Barcode and QR-Code Scanner/Handle result/BarcodeHandlingResultViewController.swift +++ b/Documentation Code Snippets/Barcode and QR-Code Scanner/Handle result/BarcodeHandlingResultViewController.swift @@ -26,6 +26,13 @@ class BarcodeHandlingResultViewController: UIViewController { extension BarcodeHandlingResultViewController: SBSDKBarcodeScannerViewControllerDelegate { + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, + didFailScanning error: any Error) { + // Handle the error. + print("Error scanning barcode: \(error.localizedDescription)") + } + + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, didScanBarcodes codes: [SBSDKBarcodeItem]) { diff --git a/Documentation Code Snippets/BarcodesBatchScanner/BarcodesBatchViewController.swift b/Documentation Code Snippets/BarcodesBatchScanner/BarcodesBatchViewController.swift index 9e7847d..060f683 100644 --- a/Documentation Code Snippets/BarcodesBatchScanner/BarcodesBatchViewController.swift +++ b/Documentation Code Snippets/BarcodesBatchScanner/BarcodesBatchViewController.swift @@ -79,7 +79,7 @@ extension BarcodesBatchViewController: SBSDKBarcodeScannerViewControllerDelegate for code in codes { // Get the source image. - let sourceImage = code.sourceImage?.toUIImage() + let sourceImage = try? code.sourceImage?.toUIImage() } } @@ -87,4 +87,10 @@ extension BarcodesBatchViewController: SBSDKBarcodeScannerViewControllerDelegate func barcodeScannerControllerShouldScanBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool { return self.shouldDetectBarcodes } + + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, + didFailScanning error: any Error) { + // Handle the error. + print("Error scanning barcode: \(error.localizedDescription)") + } } diff --git a/Documentation Code Snippets/BarcodesOverlayScanner/BarcodesOverlayViewController.swift b/Documentation Code Snippets/BarcodesOverlayScanner/BarcodesOverlayViewController.swift index 351b41d..020c33d 100644 --- a/Documentation Code Snippets/BarcodesOverlayScanner/BarcodesOverlayViewController.swift +++ b/Documentation Code Snippets/BarcodesOverlayScanner/BarcodesOverlayViewController.swift @@ -78,8 +78,7 @@ extension BarcodesOverlayViewController: SBSDKBarcodeTrackingOverlayControllerDe print(barcode) // Get the source image. - let sourceImage = barcode.sourceImage?.toUIImage() - + let sourceImage = try? barcode.sourceImage?.toUIImage() } // Implement this method if you need to customize the polygon style individually for each barcode detected. @@ -93,7 +92,9 @@ extension BarcodesOverlayViewController: SBSDKBarcodeTrackingOverlayControllerDe } // Implement this method if you need to customize the text style individually for each barcode detected. - func barcodeTrackingOverlay(_ controller: SBSDKBarcodeTrackingOverlayController, textStyleFor barcode: SBSDKBarcodeItem) -> SBSDKBarcodeTrackedViewTextStyle? { + func barcodeTrackingOverlay(_ controller: SBSDKBarcodeTrackingOverlayController, + textStyleFor barcode: SBSDKBarcodeItem, + proposedStyle: SBSDKBarcodeTrackedViewTextStyle) -> SBSDKBarcodeTrackedViewTextStyle { let style = SBSDKBarcodeTrackedViewTextStyle() if barcode.format == SBSDKBarcodeFormat.qrCode { style.textBackgroundColor = UIColor.purple.withAlphaComponent(0.2) diff --git a/Documentation Code Snippets/Data Parser/BarcodeDataParserViewController.swift b/Documentation Code Snippets/Data Parser/BarcodeDataParserViewController.swift index c908497..4992756 100644 --- a/Documentation Code Snippets/Data Parser/BarcodeDataParserViewController.swift +++ b/Documentation Code Snippets/Data Parser/BarcodeDataParserViewController.swift @@ -83,7 +83,7 @@ extension BarcodeDataParserViewController: SBSDKBarcodeScannerViewControllerDele let barcode = codes.first // Get the source image. - let sourceImage = barcode?.sourceImage?.toUIImage() + let sourceImage = try? barcode?.sourceImage?.toUIImage() // Run the parser and check the result. if let document = SBSDKBarcodeDocumentModelSwissQR(document: barcode?.extractedDocument) { @@ -101,4 +101,10 @@ extension BarcodeDataParserViewController: SBSDKBarcodeScannerViewControllerDele func barcodeScannerControllerShouldScanBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool { return true } + + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, + didFailScanning error: any Error) { + // Handle the error. + print("Error scanning barcode: \(error.localizedDescription)") + } } diff --git a/Documentation Code Snippets/Data Parser/ManualDataParser.swift b/Documentation Code Snippets/Data Parser/ManualDataParser.swift index bcc68fc..5aff9b9 100644 --- a/Documentation Code Snippets/Data Parser/ManualDataParser.swift +++ b/Documentation Code Snippets/Data Parser/ManualDataParser.swift @@ -13,25 +13,34 @@ func parseDataManually() { // Some raw barcode string. let rawBarcodeString = "(01)02804086001986(3103)000220(15)220724(30)01(3922)00198" - // Instantiate the parser. - let parser = SBSDKBarcodeDocumentParser(extractedDocumentFormats: [.gs1]) - - // Run the parser and check the result. - if let document = parser.parse(from: rawBarcodeString) { + do { + + // Instantiate the parser. + let parser = try SBSDKBarcodeDocumentParser(acceptedFormats: [.gs1]) + + // Run the parser and check the result. + let document = try parser.parse(rawString: rawBarcodeString) + + // Get the parsed document. + guard let parsedDocument = document.parsedDocument else { return } // Parse the resulted document as a GS1 document. - if let gs1ParsedDocument = SBSDKBarcodeDocumentModelGS1(document: document) { + if let gs1ParsedDocument = SBSDKBarcodeDocumentModelGS1(document: parsedDocument) { - // Retrieve the elements. - let elements = gs1ParsedDocument.elements + // Retrieve the elements. + let elements = gs1ParsedDocument.elements // Enumerate over the elements. for element in elements { - + // Do something with the element. - - print("\(element.dataTitle?.value?.text) = \(element.rawValue?.value?.text)") + if let key = element.dataTitle?.value?.text, let value = element.rawValue?.value?.text { + print("\(key) = \(value)") + } } } } + catch { + print("Error parsing document: \(error.localizedDescription)") + } } diff --git a/Documentation Code Snippets/General Scanner Configuration/BarcodeGeneralConfiguration.swift b/Documentation Code Snippets/General Scanner Configuration/BarcodeGeneralConfiguration.swift index 216d4fa..0ce52ab 100644 --- a/Documentation Code Snippets/General Scanner Configuration/BarcodeGeneralConfiguration.swift +++ b/Documentation Code Snippets/General Scanner Configuration/BarcodeGeneralConfiguration.swift @@ -91,16 +91,18 @@ class BarcodeGeneralConfiguration { // List of barcode document formats to extract. let documentFormats: [SBSDKBarcodeDocumentFormat] = [.gs1, .boardingPass, .swissQr] - // Instantiate the parser, providing the list of formats. - let parser = SBSDKBarcodeDocumentParser(extractedDocumentFormats: documentFormats) - - // Some raw barcode string. - let rawBarcodeString = "(01)02804086001986(3103)000220(15)220724(30)01(3922)00198" + do { + // Instantiate the parser, providing the list of formats. + let parser = try SBSDKBarcodeDocumentParser(acceptedFormats: documentFormats) - // Run the parser and check the result. - if let document = parser.parse(from: rawBarcodeString) { + // Some raw barcode string. + let rawBarcodeString = "(01)02804086001986(3103)000220(15)220724(30)01(3922)00198" - // Handle the result. + // Run the parser and handle the result. + let document = try parser.parse(rawString: rawBarcodeString) + } + catch { + print("Error running barcode document parser: \(error.localizedDescription)") } } diff --git a/Documentation Code Snippets/Result Handling/General/BarcodeResultHandlingExamples.swift b/Documentation Code Snippets/Result Handling/General/BarcodeResultHandlingExamples.swift new file mode 100644 index 0000000..a3aeeeb --- /dev/null +++ b/Documentation Code Snippets/Result Handling/General/BarcodeResultHandlingExamples.swift @@ -0,0 +1,146 @@ +// +// ResultHandlingExamples.swift +// ScanbotSDK Examples +// +// Created by Rana Sohaib on 19.11.25. +// + +import UIKit +import ScanbotBarcodeScannerSDK + +class BarcodeResultHandlingGeneral { + + func handleError(_ error: Error) { + + print("Error: \(error.localizedDescription)") + + // Cast `Error` to type `SBSDKError` to access underlying SDK properties. + // We can safely assume that only `SBSDKErrors` are thrown + let sdkError = error as! SBSDKError + + // Use switch to determine the error and handle it according to your use case. + switch sdkError { + case .unknownError(let string): return + case .invalidLicense(let string): return + case .nullPointer(let string): return + case .invalidArgument(let string): return + case .invalidImageRef(let string): return + case .componentUnavailable(let string):return + case .illegalState(let string): return + case .ioError(let string): return + case .invalidData(let string): return + case .operationCanceled(let string): return + case .outOfMemory(let string): return + case .timeout(let string): return + default: print("Unknown error code with description: \(sdkError.localizedDescription)") + } + } + + func checkCanceledError(_ error: Error) { + + // We can safely assume that only `SBSDKErrors` are thrown + let sdkError = error as! SBSDKError + + // Check if the error represents a canceled operation. + if sdkError.isCanceled { + print("The operation was cancelled before completion or by the user: \(error.localizedDescription)") + } + } + + func doCatchExample() { + + // The image containing a barcode. + guard let image = UIImage(named: "barcodeImage") else { return } + + do { + // Create an instance of the scanner. + let scanner = try SBSDKBarcodeScanner(configuration: .init()) + + // Create an image ref from UIImage. + let imageRef = SBSDKImageRef.fromUIImage(image: image) + + // Run scanner on the given image. + let result = try scanner.run(image: imageRef) + } + catch { + print("Error scanning barcode: \(error.localizedDescription)") + } + } + + func optionalTryExample() { + + // The image containing a barcode. + guard let image = UIImage(named: "barcodeImage") else { return } + + // Create an instance of the scanner. + let scanner = try? SBSDKBarcodeScanner(configuration: .init()) + + // Create an image ref from UIImage. + let imageRef = SBSDKImageRef.fromUIImage(image: image) + + // Run scanner on the given image. + let result = try? scanner?.run(image: imageRef) + + if let result { + // Handle the result. + + } else { + // Handle failure. + // Note: Here you do not have any information about the error contrary to the `do-catch` + // as the `error` itself is not available. + } + } +} + +// RTU-UI +class BarcodeRTUUIResultHandlingExampleViewController: UIViewController { + + func startScanning() { + + // Present the view controller modally. + SBSDKUI2BarcodeScannerViewController.present(on: self, + configuration: .init()) { controller, result, error in + if let error { + + // We can safely assume that only `SBSDKErrors` are thrown. + let sdkError = error as! SBSDKError + + // Check if the error represents a canceled operation. + if sdkError.isCanceled { + print("The operation was cancelled before completion or by the user") + + } else { + print("Error scanning barcodes: \(sdkError.localizedDescription)") + } + + } else if let result { + // Handle the result. + } + } + } +} + +// Classic +class BarcodeClassicResultHandlingExampleViewController: UIViewController, SBSDKBarcodeScannerViewControllerDelegate { + + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, + didScanBarcodes codes: [SBSDKBarcodeItem]) { + // Handle the result. + } + + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, + didFailScanning error: any Error) { + // Handle the error. + + // We can safely assume that only `SBSDKErrors` are thrown. + let sdkError = error as! SBSDKError + + // Check if the error represents a canceled operation. + if sdkError.isCanceled { + print("The operation was cancelled before completion or by the user") + + } else { + print("Error scanning barcodes: \(sdkError.localizedDescription)") + } + } +} diff --git a/Documentation Code Snippets/Result Handling/Image/BarcodeImageResultHandlingViewController.swift b/Documentation Code Snippets/Result Handling/Image/BarcodeImageResultHandlingViewController.swift index 6d4c0aa..e0c592a 100644 --- a/Documentation Code Snippets/Result Handling/Image/BarcodeImageResultHandlingViewController.swift +++ b/Documentation Code Snippets/Result Handling/Image/BarcodeImageResultHandlingViewController.swift @@ -25,18 +25,26 @@ class BarcodeImageResultHandlingViewController: UIViewController { // Configure the scanner to return barcode image. configuration.returnBarcodeImage = true - // Create the `SBSDKBarcodeScanner` instance. - let scanner = SBSDKBarcodeScanner() - - let image = UIImage(named: "test_image")! - - // Run the scanner passing the image. - let result = scanner.scan(from: image) - - // Handle the result. - result?.barcodes.forEach({ barcode in - handle(barcode: barcode) - }) + do { + // Create the scanner instance. + let scanner = try SBSDKBarcodeScanner() + + let image = UIImage(named: "test_image")! + + // Create an image ref from UIImage. + let imageRef = SBSDKImageRef.fromUIImage(image: image) + + // Run the scanner passing the image. + let result = try scanner.run(image: imageRef) + + // Handle the result. + result.barcodes.forEach({ barcode in + handle(barcode: barcode) + }) + } + catch { + print("Error running barcode scanner: \(error.localizedDescription)") + } } // Handle the resulting barcode item's image. @@ -50,14 +58,19 @@ class BarcodeImageResultHandlingViewController: UIViewController { // Since the image is of type `SBSDKImageRef`, it provides some useful operations. // e.g - // Convert to UIImage. - let uiImage = image?.toUIImage() - - // Information about the stored image. - let info = image?.info() - - // Save the image. - let success = image?.saveImage(path: "", - options: SBSDKSaveImageOptions(quality: 70, encryptionMode: .disabled)) + do { + // Convert to UIImage. + let uiImage = try image?.toUIImage() + + // Information about the stored image. + let info = try image?.info() + + // Save the image. + try image?.saveImage(path: "", + options: SBSDKSaveImageOptions(quality: 70, encryptionMode: .disabled)) + } + catch { + print("Error handling barcode image: \(error.localizedDescription)") + } } } diff --git a/Documentation Code Snippets/Result Handling/Raw values/BarcodeRawResultHandlingViewController.swift b/Documentation Code Snippets/Result Handling/Raw values/BarcodeRawResultHandlingViewController.swift index 3d3f6fe..d57d7c5 100644 --- a/Documentation Code Snippets/Result Handling/Raw values/BarcodeRawResultHandlingViewController.swift +++ b/Documentation Code Snippets/Result Handling/Raw values/BarcodeRawResultHandlingViewController.swift @@ -13,18 +13,26 @@ class BarcodeRawResultHandlingViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - // Create the `SBSDKBarcodeScanner` instance. - let scanner = SBSDKBarcodeScanner() - - let image = UIImage(named: "test_image")! - - // Run the scanner passing the image. - let result = scanner.scan(from: image) - - // Handle the result. - result?.barcodes.forEach({ barcode in - handle(barcode: barcode) - }) + do { + // Create the scanner instance. + let scanner = try SBSDKBarcodeScanner() + + let image = UIImage(named: "test_image")! + + // Create an image ref from UIImage. + let imageRef = SBSDKImageRef.fromUIImage(image: image) + + // Run the scanner passing the image. + let result = try scanner.run(image: imageRef) + + // Handle the result. + result.barcodes.forEach({ barcode in + handle(barcode: barcode) + }) + } + catch { + print("Error running barcode scanner: \(error.localizedDescription)") + } } // Handle the resulting barcode item and it's raw values. diff --git a/Podfile b/Podfile index e93058a..8860007 100644 --- a/Podfile +++ b/Podfile @@ -3,5 +3,5 @@ platform :ios, '13.0' target 'ScanbotBarcodeScannerSDKDemo' do use_frameworks! - pod 'ScanbotBarcodeScannerSDK', '7.1.4' + pod 'ScanbotBarcodeScannerSDK', '8.0.0-RC9' end diff --git a/ScanbotBarcodeScannerSDKDemo/ClassicBarcodeScanner.swift b/ScanbotBarcodeScannerSDKDemo/ClassicBarcodeScanner.swift index b91b89f..685df35 100644 --- a/ScanbotBarcodeScannerSDKDemo/ClassicBarcodeScanner.swift +++ b/ScanbotBarcodeScannerSDKDemo/ClassicBarcodeScanner.swift @@ -14,7 +14,8 @@ class ClassicBarcodeScanner: UIViewController { private var shouldScanBarcodes = true private var scannerController: SBSDKBarcodeScannerViewController! private var selectedBarcode: SBSDKBarcodeScannerResult? - + private var isShowingError: Bool = false + override func viewDidLoad() { super.viewDidLoad() @@ -48,7 +49,7 @@ class ClassicBarcodeScanner: UIViewController { return BarcodeResult(type: barcode.format, rawTextString: barcode.text, rawTextStringWithExtension: barcode.textWithExtension, - barcodeImage: barcode.sourceImage?.toUIImage(), + barcodeImage: try? barcode.sourceImage?.toUIImage(), rawBytes: barcode.rawBytes, formattedDocument: barcode.extractedDocument) }) @@ -67,6 +68,22 @@ class ClassicBarcodeScanner: UIViewController { } extension ClassicBarcodeScanner: SBSDKBarcodeScannerViewControllerDelegate { + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, didFailScanning error: any Error) { + if let error = error as? SBSDKError { + guard !isShowingError else { return } + + isShowingError = true + if error.isCanceled { + print("Scanning was cancelled by the user") + } else { + sbsdk_showError(error) { [weak self] _ in + guard let self else { return } + self.sbsdk_forceClose(animated: true, completion: nil) + } + } + } + } + func barcodeScannerControllerShouldScanBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool { return self.shouldScanBarcodes } diff --git a/ScanbotBarcodeScannerSDKDemo/ClassicBatchBarcodeScanner.swift b/ScanbotBarcodeScannerSDKDemo/ClassicBatchBarcodeScanner.swift index 41cfc8b..d315855 100644 --- a/ScanbotBarcodeScannerSDKDemo/ClassicBatchBarcodeScanner.swift +++ b/ScanbotBarcodeScannerSDKDemo/ClassicBatchBarcodeScanner.swift @@ -22,6 +22,7 @@ class ClassicBatchBarcodeScanner: UIViewController { private var scannedBarcodes: [SBSDKBarcodeItem] = [] private var scannerController: SBSDKBarcodeScannerViewController! private var isScrolling: Bool = false + private var isShowingError: Bool = false override func viewDidLoad() { super.viewDidLoad() @@ -72,6 +73,21 @@ extension ClassicBatchBarcodeScanner: SBSDKBarcodeTrackingOverlayControllerDeleg } extension ClassicBatchBarcodeScanner: SBSDKBarcodeScannerViewControllerDelegate { + func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController, didFailScanning error: any Error) { + if let error = error as? SBSDKError { + guard !isShowingError else { return } + + isShowingError = true + if error.isCanceled { + print("Scanning was cancelled by the user") + } else { + sbsdk_showError(error) { [weak self] _ in + guard let self else { return } + self.sbsdk_forceClose(animated: true, completion: nil) + } + } + } + } func barcodeScannerControllerShouldScanBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool { return !self.isScrolling @@ -106,7 +122,7 @@ extension ClassicBatchBarcodeScanner: UITableViewDataSource, UITableViewDelegate let cell = tableView.dequeueReusableCell(withIdentifier: "barcodeCell") as! ClassicBatchBarcodeScannerTableCell let code = self.scannedBarcodes[indexPath.row] - cell.barcodeImageView.image = code.sourceImage?.toUIImage() + cell.barcodeImageView.image = try? code.sourceImage?.toUIImage() cell.barcodeLabel.text = code.textWithExtension cell.barcodeTypeLabel.text = code.format.name return cell diff --git a/ScanbotBarcodeScannerSDKDemo/Main.storyboard b/ScanbotBarcodeScannerSDKDemo/Main.storyboard index 6764f8c..c24502f 100644 --- a/ScanbotBarcodeScannerSDKDemo/Main.storyboard +++ b/ScanbotBarcodeScannerSDKDemo/Main.storyboard @@ -1,9 +1,9 @@ - + - + @@ -487,7 +487,7 @@ - - + @@ -653,14 +653,14 @@ - + - + - + @@ -701,7 +701,7 @@ - + @@ -743,14 +743,14 @@ - + - +