Within my iOS
app, I am generating a QR code where the base functionality is just to move a user into a feature of the app.
I'd like to include a deep-link URL in the same QR code.
The use case would be a person that has the app shows the QR code to another person. That person would open their native camera app or a QR code scanner of choice & it would pick up this URL and be navigated to Safari / App Store.
Here is how I am creating my QR Code:
func generateQRCode(from string: String) -> UIImage? {
var jsonDict = [String: Any]()
let url = "https://mydeeplinkurlhere"
jsonDict.updateValue(url, forKey: "url")
jsonDict.updateValue(string, forKey: "xyz")
guard let jsonData = try? JSONSerialization.data(withJSONObject: jsonDict, options: [.prettyPrinted]) else {
return nil
}
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
// Input the data
qrFilter.setValue(jsonData, forKey: "inputMessage")
// Get the output image
guard let qrImage = qrFilter.outputImage else { return nil}
// Scale the image
let transform = CGAffineTransform(scaleX: 12, y: 12)
let scaledQrImage = qrImage.transformed(by: transform)
// Do some processing to get the UIImage
let context = CIContext()
guard let cgImage = context.createCGImage(scaledQrImage, from: scaledQrImage.extent) else { return nil }
let processedImage = UIImage(cgImage: cgImage)
return processedImage
}
Update:
The above code is getting me much closer to my desired result.
I am able to split this up if the QR code is scanned in the app and use what I need.
My question now is this:
If a user were to scan using their camera app, it's going to spit out what the JSON is.
So they would see both of the key-value pairs. I don't need them to see all of that. In this scenario, I'd only need them to see the URL and/or a custom message.
Can that part be edited? Meaning, what is displayed when scanning a QR Code?
Or can the website just auto-open vs. seeing the notification?
You can store any kind of object in qr code, in your situation you can send several information in your qr code by dictionary or array type, here is the code:
func generateQRCode(from dictionary: [String: String]) -> UIImage? {
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
guard let data = try? NSKeyedArchiver.archivedData(withRootObject: dictionary, requiringSecureCoding: false) else { return nil }
qrFilter.setValue(data, forKey: "inputMessage")
guard let qrImage = qrFilter.outputImage else { return nil}
let transform = CGAffineTransform(scaleX: 10, y: 10)
let scaledQrImage = qrImage.transformed(by: transform)
let context = CIContext()
guard let cgImage = context.createCGImage(scaledQrImage, from: scaledQrImage.extent) else { return nil }
let processedImage = UIImage(cgImage: cgImage)
return processedImage
}
And here is your model and how do you call that method:
let dictionary: [String: String] = [
"message" : "some message string here",
"link" : "https://google.com"
]
YOUR_IMAGE_VIEW.image = generateQRCode(from: dictionary)
You are completely free to change your data model, but scanning a qr code is different story, check it out: https://www.hackingwithswift.com/example-code/media/how-to-scan-a-qr-code
I hope that will be useful, Happy Coding 🍻