Description
BarcodeScanner is a simple and beautiful wrapper around the camera with barcode capturing functionality and a great user experience.
- Barcode scanning.
- State modes: scanning, processing, unauthorized, not found.
- Handling of camera authorization status.
- Animated focus view and custom loading indicator.
- Torch mode switch.
- Customizable colors, informational and error messages.
- No external dependencies.
- Demo project.
Table of Contents
Usage
Controller
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)
Delegates
Code delegate
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()
}
}
Error delegate
Use BarcodeScannerErrorDelegate
when you want to handle session errors.
extension ViewController: BarcodeScannerErrorDelegate {
func barcodeScanner(controller: BarcodeScannerController, didReceiveError error: ErrorType) {
print(error)
}
}
Dismissal delegate
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)
}
}
Actions
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
.
Customization
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()
Installation
BarcodeScanner is 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.
BarcodeScanner is also available through Carthage. 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.
Author
Hyper Interaktiv AS, ios@hyper.no
Contributing
We would love you to contribute to BarcodeScanner, check the CONTRIBUTING file for more info.
License
BarcodeScanner is available under the MIT license. See the LICENSE file for more info.