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

Create a vCard (Contact) QR Code

See more QR Code Examples

Demonstrates a QR code that adds a contact to a phone when scanned. The text is a vCard, the standard format for contact details: lines between BEGIN:VCARD and END:VCARD, each ending with CRLF. A vCard is fairly long, so the symbol is larger than one holding a web address. Leave out the fields that are not needed to keep it small.

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.

    //  The second argument of AppendLine is cktrue to end each line with CRLF.
    let sbCard = CkoStringBuilder()!
    sbCard.appendLine(str: "BEGIN:VCARD", crlf: true)
    sbCard.appendLine(str: "VERSION:3.0", crlf: true)
    sbCard.appendLine(str: "N:Smith;Alex;;;", crlf: true)
    sbCard.appendLine(str: "FN:Alex Smith", crlf: true)
    sbCard.appendLine(str: "ORG:Example Company", crlf: true)
    sbCard.appendLine(str: "TITLE:Sales Manager", crlf: true)
    sbCard.appendLine(str: "TEL;TYPE=CELL:+1-555-0100", crlf: true)
    sbCard.appendLine(str: "EMAIL:alex.smith@example.com", crlf: true)
    sbCard.appendLine(str: "URL:https://www.example.com", crlf: true)
    sbCard.appendLine(str: "END:VCARD", crlf: true)

    let json = CkoJsonObject()!
    json.updateSb(jsonPath: "payload.text", sb: sbCard)
    json.updateInt(jsonPath: "render.moduleSize", value: 6)
    json.updateString(jsonPath: "output.path", value: "qa_output/qr_vcard.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("vCard length: \(sbCard.length.intValue)")
    print("QR version: \(jsonOut.int(of: "symbols[0].selectedEncoding.version").intValue)")
    print("modules: \(jsonOut.int(of: "symbols[0].dimensions.modulesWide").intValue) x \(jsonOut.int(of: "symbols[0].dimensions.modulesHigh").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_vcard.png:

    //  image

    //  Sample output:

    //  vCard length: 197
    //  QR version: 10
    //  modules: 57 x 57
    //  image: 390 x 390 pixels

}