Reputation: 536
Is there any way I can add an additional string in my QR code?
I'm trying to pass two strings with keys (both are needed in my case).
So my QR code should contain authentication ID and document ID for editing a Firestore value but can't really find a good way of doing this.
func generateQR(){
let authId = Auth.auth().currentUser?.uid
if let myString = docID{
let data = myString.data(using: .ascii, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(data, forKey: "inputMessage")
let ciImage = filter?.outputImage
let transform = CGAffineTransform(scaleX: 10, y: 10)
let transformImage = (ciImage?.transformed(by: transform))
let image = UIImage(ciImage: transformImage!)
qrView.image = image
}
}
Here I'm generating my QR code and as you can see atm I just have "data" which is the document ID.
So now, somehow I need to add an additional key which should contain the "authId", is there anyway of doing this?
Function for reading the QR message
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
if metadataObjects != nil && metadataObjects.count != 0{
if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject{
if object.type == AVMetadataObject.ObjectType.qr{
let arr = object.stringValue!.components(separatedBy:",")
let docId = arr.first!
let authId = arr[1]
let alert = UIAlertController.init(title: "QR Code", message: "Vill du använda denna QR koden?", preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Ny", style: .cancel, handler: nil))
alert.addAction(UIAlertAction.init(title: "Använd", style: .default, handler: { (nil) in
self.removeDiscountFromFirestore(documentId: docId, authentication: authId)
}))
self.present(alert, animated: true)
}
}
}
}
So how can I pass 2 strings in my QR code? Thanks in advance.
After Sh_Khan's input I managed to pass the data over, but they become optional and cannot be forced with "!". So I fixed this by adding
let data = "\(String(describing: docID!)),\(authId!)".data(using: .ascii, allowLossyConversion: false)
Where I generate the QR code.
Upvotes: 2
Views: 278
Reputation: 100549
You can try to concatenate them with say a ,
let data = "\(docId),\(authId)".data(using: .ascii, allowLossyConversion: false)
And when you read the qr code separate them
let arr = object.stringValue!.components(separatedBy:",")
let docId = arr.first!
let authId = arr[1]
Upvotes: 2