Search code examples
swiftxcodeavfoundationbarcode-scanner

xcode avfoundation barcode sanner how to return type of scanned code


Xcode 12 / Swift 5

AVfoundation

I followed this tutorial for barcode scanner http://www.wepstech.com/bar-qr-code-ios-with-swift-5/

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {     
    if let metadataObject = metadataObjects.first {
        guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
        guard let stringValue = readableObject.stringValue else { return }
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        found(code: stringValue)
        captureSession.stopRunning()
        parent.presentationMode.wrappedValue.dismiss()
    }
}

It returns the scanned value,

but how to get the barcode TYPE ; to know if it scanned qrcode, code128...


Solution

  • You can switch your metadataObject's type property and check which one you've got:

    switch metadataObject.type {
    case .qr:
          print("qrcode")
     case .code128:
          print("code128")
     default:
          print("other type")
    }