Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.7.0+

Generate a QR Code PNG File

See more QR Code Examples

Demonstrates the simplest way to create a QR code: the text to encode goes in payload.text, and output.path names the PNG file to write. QrCode chooses the smallest QR version that holds the text at error-correction level M, and the response reports what it chose and the size of the image. The request and the response are both JSON objects, and they must be two different objects.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "payload.text", value: "https://www.chilkatsoft.com/")

    //  Each module (one of the small squares) is drawn 8 pixels wide.  The default is 4.
    json.updateInt(jsonPath: "render.moduleSize", value: 8)

    //  Write the image to a file.  An existing file is replaced only when output.overwrite is true.
    //  Otherwise the call fails and the existing file is left as it was.
    json.updateString(jsonPath: "output.path", value: "qa_output/qr_chilkat.png")
    json.updateBool(jsonPath: "output.overwrite", value: true)

    //  The image is also returned in the response as base64 unless output.return is "none".
    json.updateString(jsonPath: "output.return", value: "none")

    let fmt = CkoFormats()!
    let jsonOut = CkoJsonObject()!
    success = fmt.qrCode(op: "generate", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(fmt.lastErrorText!)")
        return
    }

    print("file: \(jsonOut.string(of: "outputs[0].path")!)")
    print("image: \(jsonOut.int(of: "outputs[0].width").intValue) x \(jsonOut.int(of: "outputs[0].height").intValue) pixels, \(jsonOut.int(of: "outputs[0].byteCount").intValue) bytes")
    print("QR version: \(jsonOut.int(of: "symbols[0].selectedEncoding.version").intValue)")
    print("error correction: \(jsonOut.string(of: "symbols[0].selectedEncoding.errorCorrection")!)")
    print("modules: \(jsonOut.int(of: "symbols[0].dimensions.modulesWide").intValue) x \(jsonOut.int(of: "symbols[0].dimensions.modulesHigh").intValue)")

    //  The image written to qa_output/qr_chilkat.png:

    //  image

    //  Sample output:

    //  file: qa_output/qr_chilkat.png
    //  image: 296 x 296 pixels, 1251 bytes
    //  QR version: 3
    //  error correction: M
    //  modules: 29 x 29

}