BarcodeScanneris a simple and beautiful wrapper around the camera with barcode capturing functionality and a great user experience.
To start capturing just instantiate BarcodeScannerController
, set needed delegates and present it:
let controller = BarcodeScannerController() controller.codeDelegate = self controller.errorDelegate = self controller.dismissalDelegate = self presentViewController(controller, animated: true, completion: nil)
You can also push BarcodeScannerController
to your navigation stack:
let controller = BarcodeScannerController() controller.codeDelegate = self navigationController?.pushViewController(controller, animated: true)
Use BarcodeScannerCodeDelegate
when you want to get the captured code back.
extension ViewController: BarcodeScannerCodeDelegate { func barcodeScanner(controller: BarcodeScannerController, didCapturedCode code: String) { print(code) controller.reset() } }
Use BarcodeScannerErrorDelegate
when you want to handle session errors.
extension ViewController: BarcodeScannerErrorDelegate { func barcodeScanner(controller: BarcodeScannerController, didReceiveError error: ErrorType) { print(error) } }
Use BarcodeScannerDismissalDelegate
to handle "Close button" tap. Please note that BarcodeScannerController
doesn't dismiss itself if it was presented initially.
extension ViewController: BarcodeScannerDismissalDelegate { func barcodeScannerDidDismiss(controller: BarcodeScannerController) { controller.dismissViewControllerAnimated(true, completion: nil) } }
When the code is captured BarcodeScannerController
switches to the processing mode:
While the user see a nice loading animation you can perform some background task, for example make a network request to fetch product info based on the code. When the task is done you have 3 options to proceed:
Dismiss BarcodeScannerController
and show your results.
func barcodeScanner(controller: BarcodeScannerController, didCapturedCode code: String) { // Code processing controller.dismissViewControllerAnimated(true, completion: nil) }
Show an error message and switch back to the scanning mode (for example, when there is no product found with a given barcode in your database):
func barcodeScanner(controller: BarcodeScannerController, didCapturedCode code: String) { // Code processing controller.resetWithError("Error message") // If message is not provided the default message from the config will be used instead. }
Reset the controller to the scanning mode (with or without animation):
func barcodeScanner(controller: BarcodeScannerController, didCapturedCode code: String) { // Code processing controller.reset(animated: true) }
If you want to do continuous barcode scanning just set the oneTimeSearch
property on your BarcodeScannerController
instance to false
.
We styled BarcodeScanner to make it look nice, but feel free to customize its appearance by changing global configuration variables:
// Strings BarcodeScanner.Title.text = NSLocalizedString("Scan barcode", comment: "") BarcodeScanner.CloseButton.text = NSLocalizedString("Close", comment: "") BarcodeScanner.SettingsButton.text = NSLocalizedString("Settings", comment: "") BarcodeScanner.Info.text = NSLocalizedString( "Place the barcode within the window to scan. The search will start automatically.", comment: "") BarcodeScanner.Info.loadingText = NSLocalizedString("Looking for your product...", comment: "") BarcodeScanner.Info.notFoundText = NSLocalizedString("No product found.", comment: "") BarcodeScanner.Info.settingsText = NSLocalizedString( "In order to scan barcodes you have to allow camera under your settings.", comment: "") // Fonts BarcodeScanner.Title.font = UIFont.boldSystemFontOfSize(17) BarcodeScanner.CloseButton.font = UIFont.boldSystemFontOfSize(17) BarcodeScanner.SettingsButton.font = UIFont.boldSystemFontOfSize(17) BarcodeScanner.Info.font = UIFont.boldSystemFontOfSize(14) BarcodeScanner.Info.loadingFont = UIFont.boldSystemFontOfSize(16) // Colors BarcodeScanner.Title.color = UIColor.blackColor() BarcodeScanner.CloseButton.color = UIColor.blackColor() BarcodeScanner.SettingsButton.color = UIColor.whiteColor() BarcodeScanner.Info.textColor = UIColor.blackColor() BarcodeScanner.Info.tint = UIColor.blackColor() BarcodeScanner.Info.loadingTint = UIColor.blackColor() BarcodeScanner.Info.notFoundTint = UIColor.redColor()
BarcodeScanneris available through CocoaPods . To install it, simply add the following line to your Podfile:
pod 'BarcodeScanner'
In order to quickly try the demo project of a BarcodeScanner just run pod try BarcodeScanner
in your terminal.
BarcodeScanneris also available throughCarthage. To install just write into your Cartfile:
github "hyperoslo/BarcodeScanner"
To install BarcodeScanner manually just download and drop Sources
and Images
folders in your project.
Hyper Interaktiv AS, ios@hyper.no
We would love you to contribute to BarcodeScanner , check theCONTRIBUTING file for more info.
BarcodeScanneris available under the MIT license. See theLICENSE file for more info.