Skip to content

A tiny but versatile QR scanner written in Swift

License

Notifications You must be signed in to change notification settings

carsonhawley/Cicada

Repository files navigation

Cicada

A tiny but versatile QR scanner written in Swift

iOS Platform Swift Language MIT License

Installation

Cocoapods

Add the following package to your Podfile

platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  pod 'Cicada', '~> 0.1.0'
end

Quickstart

Cicada is designed to be as hands-off as possible. If you want a quick n' dirty example:

import Cicada

class ExampleViewController: UIViewController {
    
    @IBOutlet private var previewView: UIView!
    
    private let capture = Capture(types: [.qr], mode: .once)

    override func viewDidLoad() {
        super.viewDidLoad()
        
        capture.start(preview: previewView) { result in
            switch result {
            case .success(let codes):
                print("capture result: \(codes.first!.stringValue)")
            case .failure(let error):
                print("do something with error")
            }
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        capture.stop()
    }
}

And if delegates are more your style:

class ExampleViewController: UIViewController, CicadaCaptureDelegate {
    
    func capture(_ capture: Capture, didReceive result: Result) {
        // handle the result here
    }
}

Remember to include NSCameraUsageDescription in your info.plist before attempting to access the camera.

Cicada supports several different capture behaviors

  • .once - Returns a single code. This is the default case.
  • .onceUnique - Scans for many codes and returns each unique code once
  • .continuous - Returns all codes until capture.stop() is called. Codes are streamed at a 0.2 second interval to preserve battery life

Scan Area

You can limit the area of the screen where codes are detected. The most common example is scanning within a viewfinder frame:

capture.scanArea = { self.viewfinder.frame }

Show the Torch

Toggle the torch manually by calling:

capture.toggleTorch(on: true)

...or allow the system to enable the torch automatically as needed:

let capture = Capture(types: [.qr], autoTorch: true)

Haptic Feedback

You can choose to vibrate the device when a code is detected. The list of available styles from least to most impactful are:

HapticStyle.light
HapticStyle.medium
HapticStyle.heavy
HapticStyle.double 

All haptic feedback styles are ignored for .continuous mode

Camera Orientation

If your app supports multiple orientations, then you must update the capture preview to reflect those changes:

override func viewDidLayoutSubviews() {
    capture.autoResizePreview()
}

Or if your parent is UIView:

override func layoutSubviews() {
    super.layoutSubviews()
    capture.autoResizePreview()
}

SwiftUI

Cicada also provides its own SwiftUI view

struct ExampleView: View {

    var body: some View {
        CaptureView(mode: .once) { result in
            switch result {
            case .success(let codes):
                codes.forEach { result in
                    print("Capture result: \(result.stringValue)")
                }
            case .failure(let error):
                print("An error occured: \(error.localizedDescription)")
            }
        }
    }
}

License

Cicada is available under the MIT license. See the LICENSE file for more info.

About

A tiny but versatile QR scanner written in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published