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.
-
The close button which is on the left of the navigation bar.
Declaration
Swift
@IBOutlet public private(set) weak var closeButton: UIBarButtonItem!
-
The
FUIBarcodeScanner
in this view controller.Declaration
Swift
public var barcodeScanner: FUIBarcodeScanner! { get set }
-
The delegate for this
FUIBarcodeScanViewController
.Declaration
Swift
public weak var delegate: FUIBarcodeScanViewControllerDelegate?
-
The effective
UINavigationController
.Developer could set this to the
UINavigationController
for this view controller. If developer did not set this value, theUINavigationController
of this view controller will be returned if it is not nil. Otherwise, the rootUINavigationController
from thekeyWindow
, if any, will be returned.Declaration
Swift
public var effectiveNavigationController: UINavigationController? { get set }
-
A code block that whill be invoked when the barcode scan view is closed.
Declaration
Swift
public var onCloseBarcodeScanView: (() -> Void)?
-
Creates a
FUIBarcodeScanViewController
object from storyboard.Declaration
Swift
public class func createInstanceFromStoryboard() -> FUIBarcodeScanViewController
Return Value
The instantiated
FUIBarcodeScanViewController
object. -
The scanner is stopped after it received a scan result. Call this function to start another scan.
Declaration
Swift
public func restartScan()