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

Validate a QR Code Request

See more QR Code Examples

Demonstrates the validate operation, which examines a request without producing anything. The scope says how far to look: definition checks the options, capacity also checks that the data fits a QR symbol, and layout also checks the rendered size against the limits. The response has a validationStatus of valid or invalid, a list of checks, and findings[] describing what is wrong. A request that cannot be understood at all, such as one with a misspelled option, still makes the call fail in the usual way.

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 fmt = CkoFormats()!
    let jsonOut = CkoJsonObject()!
    var i: Int
    var n: Int

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "target", value: "symbol")
    json.updateString(jsonPath: "validation.scope", value: "capacity")
    json.updateString(jsonPath: "payload.text", value: "https://www.chilkatsoft.com/barcode_overview.asp")

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

    print("status: \(jsonOut.string(of: "validationStatus")!)")
    n = jsonOut.size(ofArray: "checks").intValue
    for i = 0; i <= n - 1; i++ {
        jsonOut.i = i
        print("  \(jsonOut.string(of: "checks[i].name")!): \(jsonOut.string(of: "checks[i].status")!)")
    }

    //  The same text does not fit version 2 at error-correction level H.
    json.updateInt(jsonPath: "encoding.qr.maxVersion", value: 2)
    json.updateString(jsonPath: "encoding.qr.errorCorrection", value: "H")

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

    print("status: \(jsonOut.string(of: "validationStatus")!)")
    n = jsonOut.size(ofArray: "checks").intValue
    for i = 0; i <= n - 1; i++ {
        jsonOut.i = i
        print("  \(jsonOut.string(of: "checks[i].name")!): \(jsonOut.string(of: "checks[i].status")!)")
    }

    n = jsonOut.size(ofArray: "findings").intValue
    for i = 0; i <= n - 1; i++ {
        jsonOut.i = i
        print("finding: \(jsonOut.string(of: "findings[i].code")!) at \(jsonOut.string(of: "findings[i].path")!)")
        print("  \(jsonOut.string(of: "findings[i].message")!)")
    }

    //  Sample output:

    //  status: valid
    //    definition: passed
    //    capacity: passed
    //    layout: notApplicable
    //  status: invalid
    //    definition: passed
    //    capacity: failed
    //    layout: notApplicable
    //  finding: capacityExceeded at /payload
    //    the payload does not fit any admitted QR version at the selected error-correction level

}