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

Create a Wi-Fi QR Code

See more QR Code Examples

Demonstrates a QR code that joins a phone to a Wi-Fi network when scanned. A QR code only holds text. Phones recognize text of the form WIFI:T:;S:;P:;; and offer to join the network. T is WPA (also used for WPA2 and WPA3), WEP, or nopass for an open network, and H:true can be added for a hidden network. In the name and the password, the characters backslash, semicolon, comma, colon and double quote must each be preceded by a backslash.

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: "WIFI:T:WPA;S:Chilkat Guest;P:correct-horse-battery-staple;;")

    //  Level Q, so that the code still scans when it is printed small or gets worn.
    json.updateString(jsonPath: "encoding.qr.errorCorrection", value: "Q")
    json.updateInt(jsonPath: "render.moduleSize", value: 8)

    //  Say which network it is under the code.
    json.updateBool(jsonPath: "render.text.enabled", value: true)
    json.updateString(jsonPath: "render.text.value", value: "Wi-Fi: Chilkat Guest")
    json.updateInt(jsonPath: "render.text.scale", value: 2)

    json.updateString(jsonPath: "output.path", value: "qa_output/qr_wifi.png")
    json.updateBool(jsonPath: "output.overwrite", value: true)
    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("QR version: \(jsonOut.int(of: "symbols[0].selectedEncoding.version").intValue)")
    print("image: \(jsonOut.int(of: "outputs[0].width").intValue) x \(jsonOut.int(of: "outputs[0].height").intValue) pixels")

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

    //  image

    //  Sample output:

    //  file: qa_output/qr_wifi.png
    //  QR version: 5
    //  image: 360 x 374 pixels

}