Search code examples
iosswiftqr-code

Can I generate a QR code that contains both URL and text values?


This question has been asked, but I'm not quite asking the same thing. Using iOS Swift, I am trying to store 2 values in a QR code. One is a URL of an app on the store. The other is a string that can be picked up by that app (it has its own scanner with logic to obtain the string value). The second part works fine, as I can easily parse the whole string. I tried putting a comma between the values, and it almost works, but I get a "Can't connect to the App Store" message when I use a generic scanner. It picks up the URL and tries to connect, but the extra data seems to screw it up. If I take out the comma and string, the URL then works.

Here is a subset of my code...

override func viewDidLoad() {
    super.viewDidLoad()

    let payload = "https://apps.apple.com/ca/app/.../,<my string value>"
    let image = generateQRCode(from: payload)
    qrCodeImage.image = image
}


func generateQRCode(from string: String) -> UIImage? {
    let data = string.data(using: String.Encoding.ascii)

    if let filter = CIFilter(name: "CIQRCodeGenerator") {
        filter.setValue(data, forKey: "inputMessage")
        let transform = CGAffineTransform(scaleX: 3, y: 3)

        if let output = filter.outputImage?.transformed(by: transform) {
            return UIImage(ciImage: output)
        }
    }

    return nil
}

Does anyone know if this is even possible? e.g. could I use json or a vcard, or would the generic scanner not be able to pick out the URL?


Solution

  • You can add the data you want as a query parameter on the QR code's URL, unless there are privacy issues with the data you're appending to the URL.