xcode avfoundation 条码扫描器如何 return 扫描代码的类型

xcode avfoundation barcode sanner how to return type of scanned code

Xcode 12 / Swift 5

AV 基金会

我按照条形码扫描仪教程学习了 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()
    }
}

是returns扫描的值,

但是如何获取条码类型;知道它是否扫描了二维码,code128...

您可以切换 metadataObjecttype 属性 并查看您拥有的是哪一个:

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