QRCodeReader

public class QRCodeReader : NSObject, AVCaptureMetadataOutputObjectsDelegate

This class can be used to conveniently read QR codes from the integrated camera of the device. By default the first QR code detected is returned as a string. If manualSelection is enabled, the user can choose one of the visible QR codes manually. The user is given indication how to enable permissions is denied or restricted.

  • A custom UIButton that is used to select specific QR codes.

    See more

    Declaration

    Swift

    private class ResultButton : UIButton
  • This enum indicates possible errors that can occur during capturing a QR code.

    See more

    Declaration

    Swift

    public enum Error : Swift.Error
  • This is a convenience type for the callback that is executed if the scan did finish.

    Declaration

    Swift

    public typealias FinishedScan = (String?) -> Void
  • This sets the color of the text of the Cancel button.

    Note

    This needs to be set before calling startScaning.

    Declaration

    Swift

    public var cancelButtonTextColor: UIColor?
  • This sets the background color of the Cancel button.

    Note

    This needs to be set before calling startScaning.

    Declaration

    Swift

    public var cancelButtonBackgroundColor: UIColor
  • This sets the height of the Cancel button.

    Note

    This needs to be set before calling startScaning.

    Declaration

    Swift

    public var cancelButtonHeight: CGFloat
  • This sets the margin around the Cancel button.

    Note

    This needs to be set before calling startScaning.

    Declaration

    Swift

    public var cancelButtonMargin: CGFloat
  • This enables vibrating the device or blinking the screen (if vibration is not supported) upon successful detection of a QR code.

    Declaration

    Swift

    public var feedbackOnSuccess: Bool
  • This enables the possibility to select one of the visible QR codes manually. If this is disabled, the scan will automatically finish and select the first QR code detected.

    Declaration

    Swift

    public var manualSelection: Bool
  • The function that is invoked if the scan is finished.

    Declaration

    Swift

    private var callback: QRCodeReader.FinishedScan?
  • The layer that is showing the actual video input from the camera, so that the user sees where he is pointing the device.

    Declaration

    Swift

    private var videoLayer: AVCaptureVideoPreviewLayer!
  • The current capturing session.

    Declaration

    Swift

    private var session: AVCaptureSession!
  • The cancel button.

    Declaration

    Swift

    private var cancelButton: UIButton!
  • The buttons for all QR codes currently visible.

    Declaration

    Swift

    private var resultButtons: [UIButton]
  • Upon deinitalization the scan is aborted.

    Declaration

    Swift

    deinit
  • Remove all currently visible result buttons.

    Declaration

    Swift

    private func removeResultButtons()
  • Aborts a current scan.

    Declaration

    Swift

    @objc
    public func stopScanning()
  • Select a result based on a UI event triggered by interacting with a ResultButton.

    Declaration

    Swift

    @objc
    private func selectResult(_ sender: ResultButton)

    Parameters

    sender

    The result button that triggered the event.

  • Select a string value of a QR code as result and finish the scan.]

    • paramters:
      • qrCode: The selected QR code.

    Declaration

    Swift

    private func selectResult(qrCode: String)
  • Starts scanning for a QR code.

    This will activate the default camera, which is the camera on the backside of the iOS device. The user will be asked to grant permissions if this did not happen already. If the user previously had denied access to the camera or access is restricted on the device in general instructions for enabling the camere will be provided to the user.

    Note

    You need to set the NSCameraUsageDescription in your Info.plist else the application will crash when trying to access the camera.

    Precondition

    You must not call this method until a previous scan has finished.

    Declaration

    Swift

    public func startScanning(_ finishedScan: @escaping FinishedScan) throws

    Parameters

    finishedScan

    A callback that is invoked once the scan has finished. The argument will be the QR code if that was detected successfully, nil else, i.e., if the user canceled the scan.

  • See

    AVCaptureMetadataOutputObjectsDelegate

    Declaration

    Swift

    public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)