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

Generate a QR Code as SVG

See more QR Code Examples

Demonstrates SVG output. An SVG is a vector image, so it stays sharp at any size, which suits print and responsive web pages. Setting output.format to svg and output.return to text returns the SVG document as a string in outputs[0].text. The SVG can also be written straight to a file with output.path, the same as a PNG.

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/")
    json.updateString(jsonPath: "output.format", value: "svg")

    //  An SVG is text, so it can be returned as text rather than as base64.
    json.updateString(jsonPath: "output.return", value: "text")

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

    let sbSvg = CkoStringBuilder()!
    jsonOut.string(ofSb: "outputs[0].text", sb: sbSvg)

    print("MIME type: \(jsonOut.string(of: "outputs[0].mimeType")!)")
    print("size: \(jsonOut.int(of: "outputs[0].width").intValue) x \(jsonOut.int(of: "outputs[0].height").intValue) \(jsonOut.string(of: "outputs[0].units")!)")
    print("SVG length: \(sbSvg.length.intValue) characters")

    //  The SVG is a complete document that can be saved, or placed inline in an HTML page.
    success = sbSvg.writeFile(path: "qa_output/qr_chilkat.svg", charset: "utf-8", emitBom: false)
    if success == false {
        print("\(sbSvg.lastErrorText!)")
        return
    }

    //  The SVG:

    //  image

    //  Sample output:

    //  MIME type: image/svg+xml
    //  size: 148 x 148 px
    //  SVG length: 12500 characters

}