I am trying to scan a qr code that I have generated using Adding Multiple Key-Value Pairs to QR Code as a guide. I can generate the qr code, but when I try to scan it, it yields a metadataObj.stringValue of null.
Here is my code to read the metadata output:
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == [] || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
print("No QR code is detected")
return
}
// Get the metadata object.
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if metadataObj.type == AVMetadataObject.ObjectType.qr {
// If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
if let layer = previewLayer{
let barCodeObject = layer.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds
}
guard let inputData = metadataObj.stringValue?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false),
let dictionary = NSKeyedUnarchiver.unarchiveObject(with: inputData) as? [String: NSData] else { return }
print(dictionary["firstName"] ?? "None")
}
}
How do I unarchive the data if the meta data object is outputting a string value of null?