CodeHunter
What it does
CodeHunter is a simple, drop in project for creating any kind of bar code scanner with an iOS device.
Requirements
- iOS 9.0+
- Version 3.x
- XCode 10.x & Swift 4.2+
Where are the older versions?
I have not maintained this in quite a while, so when I went to update things - everything went wrong. Since nothing wanted to talk to anything, and I am not interested in spending my day fighting the toolchain, a clean slate was the easiest approach.
Since I had to register an entirely new pod, it must now be installed with Code_Hunter, and not the previous CodeHunter.
Installation
CodeHunter is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Code_Hunter'
Instructions
Import the framework:
import CodeHunter
Inherit CodeHunterViewController
You must inherit the CodeHunterViewController class into your ViewController to allow for support of events such as screen rotations.
class ViewController: CodeHunterViewController
Setup scanner
You can set up a default scanner session that will only target QR codes with the following command:
setupCaptureSession()
If you require a different barcode type, specify it inside the setupCaptureSession function:
setupCaptureSession(.typePDF417)
You can also set up multiple types:
setupCaptureSession(.typePDF417, .type128)
There is an optional value to have the scanner wait a further command to begin scanning:
setupCaptureSession(autoStart: false, Barcode.type128)
You can then start the scanner manually:
startSession()
Delegate
Set the delegate so it can respond to events:
self.delegate = self
If you implement the cancel button, you will also want to use the transition delegate:
self.transitionDelegate = self
Configure acceptable barcodes
It is possible to limit the values accepted by the scanner, to certain strings, numeric values, or any other value you can express through a regular expression.
This accepts any amount of numbers:
matchCode(lettersAllowed: false, numbersAllowed: true)
Important note on length values
Due to how iOS fails to handle large values (and the impracticality of them anyways) when processing RegEx expressions, the numeric values are Int16 to prevent excessive values from failing due to this.
This accepts 0-20 lower case letters, but no capital letters or numbers:
matchCode(lettersAllowed: true, allowCapitalLetters: false, numbersAllowed: false, minLength: 0, maxLength: 20)
This uses an expression you define. For this example, it will only accept conventional web addresses
matchCodeWithExpression(regularExpression: "((https|http)://)((\\w|-)+)(([.]|[/])((\\w|-)+))+")
This one will fail because the max length is 0, which triggers the scanner:error: delegate method:
matchCode(lettersAllowed: true, allowCapitalLetters: false, numbersAllowed: false, minLength: 0, maxLength: 0)
This one will fail because the max length is longer than the min length, which triggers the scanner:error: delegate method
matchCode(lettersAllowed: true, allowCapitalLetters: false, numbersAllowed: false, minLength: 10, maxLength: 0)
Border
You can enable an optional border around the screen.
Provides a border around the scanner window with default values of 20.0 width, black color and 0.5 opacity
setupBorder()
Provides a border around the scanner window using provided values for width, color and opacity
setupBorder(width: 60.0, color: .gray, opacity: 0.6)
Cancel button
There is an optional cancel button you can place at the bottom of the screen.
You can add a cancel button using the default command below. It will stop the scan & dismiss the view controller when pressed
setupCancelButton()
You can modify the cancel button with a custom message, title color and border parameters. When pressed, it will terminate scan & close view
let cancelBorderProperties = CancelBorderProperties(borderColor: .black, borderWidth: 1.0, cornerRadius: 5.0)
setupCancelButton(title: "Cancel", titleColor: .red, cancelBorderProperties: cancelBorderProperties)
Screenshots
For taking screenshots, you can provide an image that will be placed behind any border or button you have enabled
setupScreenshot(with: image)
Handling camera access restrictions
CodeHunter checks for camera access during setup, as well as when starting to scan. If camera access is denied, it will return the CodeHunter.notAuthorized error.
Changelog
Changelog | See the changes introduced in each version.
Author
Sean McNeil
License
CodeHunter is available under the MIT license. See the LICENSE file for more info.