Skip to content

Barcode Scan View

FUIBarcodeScanViewController

public class FUIBarcodeScanViewController: UIViewController, FUIBarcodeScannerDelegate

This UIViewController is to be used for user to scan for 1D and 2D codes using the device's camera.

Developer may use segue in their storyboard to access this FUIBarcodeScanViewController in FUIBarcodeScanViewController.storyboard. The delegate property of the FUIBarcodeScanViewController and the properties of the FUIBarcodeScanner may be set in the prepare for segue function as follows:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination as! UINavigationController
let vc = destination.viewControllers[0]
if vc is FUIBarcodeScanViewController {
let scanViewController = vc as! FUIBarcodeScanViewController
switch segue.identifier! {
case "CustomScanSegue":
scanViewController.barcodeScanner.scanMode = .qr
scanViewController.barcodeScanner.indicatorBorderColor = UIColor.red.cgColor
scanViewController.barcodeScanner.indicatorBorderWidth = 20
scanViewController.barcodeScanner.promptMessage = "Scan A QR Code"
scanViewController.barcodeScanner.scanResultTransformer = { s in
return s + "-transformed"
}
scanViewController.delegate = self
default:
break
}
}
}

Or, developer may use codes to instantiate this controller as follows:

func customScanFromCode(_ sender: Any) {
let scanViewController = FUIBarcodeScanViewController.createInstanceFromStoryboard()
scanViewController.barcodeScanner.scanMode = .qr
scanViewController.barcodeScanner.indicatorBorderColor = UIColor.red.cgColor
scanViewController.barcodeScanner.indicatorBorderWidth = 20
scanViewController.barcodeScanner.promptMessage = "Scan A QR Code"
scanViewController.barcodeScanner.scanResultTransformer = { s in
return "transformed"
}
scanViewController.delegate = self

let navController = UINavigationController(rootViewController: scanViewController)
self.navigationController?.present(navController, animated: true, completion: nil)
}

Also, developer needs to implement the FUIBarcodeScanViewControllerDelegate protocol to be notified with a scan result:

func barcodeScanViewController(_ barcodeScanViewController: FUIBarcodeScanViewController, didReceiveScanResult scanResult: FUIBarcodeScanResult?) {
print("scan result: \(String(describing: scanResult?.scanResultString))")
if scanResult?.scanResultString != "success" {
alertInvalidQRCode()
} else {
barcodeScanViewController.dismiss(animated: true, completion: nil)
}
}

Attention

The delegate object with type FUIBarcodeScanViewControllerDelegate is declared as a weak reference. So on deallocation it will be automatically set to nil. To keep it alive as expected, developer should retain the delegate object during the whole execution scope.


Last update: April 14, 2021