/BarcodeScanner

:mag_right: A simple and beautiful barcode scanner.

Primary LanguageSwiftOtherNOASSERTION

BarcodeScanner

CI Status Version Swift Carthage Compatible License Platform

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

BarcodeScanner Icon

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

present(controller, animated: true, completion: nil)
BarcodeScanner scanning

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, didCaptureCode code: String, type: 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: Error) {
    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.dismiss(animated: true, completion: nil)
  }
}

Actions

When the code is captured BarcodeScannerController switches to the processing mode:

BarcodeScanner loading

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:

  1. Dismiss BarcodeScannerController and show your results.
func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
  // Code processing
  controller.dismiss(animated: true, completion: nil)
}
  1. 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):
BarcodeScanner error

func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
  // Code processing
  controller.resetWithError(message: "Error message")
  // If message is not provided the default message from the config will be used instead.
}
  1. Reset the controller to the scanning mode (with or without animation):
func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
  // Code processing
  controller.reset(animated: true)
}

If you want to do continuous barcode scanning just set the isOneTimeSearch 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.boldSystemFont(ofSize: 17)
BarcodeScanner.CloseButton.font = UIFont.boldSystemFont(ofSize: 17)
BarcodeScanner.SettingsButton.font = UIFont.boldSystemFont(ofSize: 17)
BarcodeScanner.Info.font = UIFont.boldSystemFont(ofSize: 14)
BarcodeScanner.Info.loadingFont = UIFont.boldSystemFont(ofSize: 16)

// Colors
BarcodeScanner.Title.color = UIColor.black
BarcodeScanner.CloseButton.color = UIColor.black
BarcodeScanner.SettingsButton.color = UIColor.white
BarcodeScanner.Info.textColor = UIColor.black
BarcodeScanner.Info.tint = UIColor.black
BarcodeScanner.Info.loadingTint = UIColor.black
BarcodeScanner.Info.notFoundTint = UIColor.red

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.