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 width of the scan guides image for regular sized devices in pixels.
The default is 640 px.
Declaration
Swift
public var scanGuidesRegularWidth: CGFloat { get set }
-
The height of the scan guides image for regular sized devices in pixels.
The default is 361 px.
Declaration
Swift
public var scanGuidesRegularHeight: CGFloat { get set }
-
The width of the scan guides image for compact sized devices in pixels.
The default is 343 px.
Declaration
Swift
public var scanGuidesCompactWidth: CGFloat { get set }
-
The height of the scan guides image for compact sized devices in pixels.
The default is 194 px.
Declaration
Swift
public var scanGuidesCompactHeight: CGFloat { get set }
-
The close button which is on the left of the navigation bar.
Declaration
Swift
@IBOutlet public private(set) weak var closeButton: UIBarButtonItem! { get }
-
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)?
-
This property indicates if the scanner session is to automatically restart after a barcode has been discovered.
If this property is
true
, the scanner session is restarted after the delegate’sdidReceiveScanResult
function is invoked. Otherwise, the delegate implementation is responsible to restart the scanner session if desired.The default is true.
Declaration
Swift
public var isAutoRestart: Bool
-
A custom view to be displayed when a scan result is validated by the delegate.
The default is
nil
. The developer needs to provide this custom view when appropriate.When this property is not
nil
, the delegate’s functionfunc barcodeScanViewController(_ barcodeScanViewController: FUIBarcodeScanViewController, didReceiveScanResult scanResult: FUIBarcodeScanResult?, confirmationView: UIView) -> Bool
will be invoked and the
confirmationView
will be displayed when the function returnstrue
.Declaration
Swift
public var confirmationView: UIView?
-
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()