Swift Requires Chilkat v11.7.0+
Swift
Handle QR Code Errors
See more QR Code Examples
Demonstrates what happens when a request is refused. QrCode returns false and the response holds errors[0] with three members: a code that does not change between versions, the path of the request member at fault (a JSON Pointer such as /encoding/qr/version), and a message. Requests are checked strictly, so a misspelled option is an error rather than being silently ignored. LastErrorText has the same information in more detail.Chilkat Swift Downloads
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 json = CkoJsonObject()!
let jsonOut = CkoJsonObject()!
// 1. A misspelled option: "moduleSise" for "moduleSize".
json.updateString(jsonPath: "payload.text", value: "https://www.chilkatsoft.com/")
json.updateInt(jsonPath: "render.moduleSise", value: 8)
success = fmt.qrCode(op: "generate", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("code: \(jsonOut.string(of: "errors[0].code")!)")
print("path: \(jsonOut.string(of: "errors[0].path")!)")
print("message: \(jsonOut.string(of: "errors[0].message")!)")
}
// 2. A value out of range: QR versions are 1 to 40.
json.clear()
json.updateString(jsonPath: "payload.text", value: "https://www.chilkatsoft.com/")
json.updateInt(jsonPath: "encoding.qr.version", value: 41)
success = fmt.qrCode(op: "generate", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("code: \(jsonOut.string(of: "errors[0].code")!)")
print("path: \(jsonOut.string(of: "errors[0].path")!)")
print("message: \(jsonOut.string(of: "errors[0].message")!)")
}
// 3. Too much data: this text does not fit a version 1 symbol.
json.clear()
json.updateString(jsonPath: "payload.text", value: "https://www.chilkatsoft.com/barcode_overview.asp")
json.updateInt(jsonPath: "encoding.qr.version", value: 1)
success = fmt.qrCode(op: "generate", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("code: \(jsonOut.string(of: "errors[0].code")!)")
print("path: \(jsonOut.string(of: "errors[0].path")!)")
print("message: \(jsonOut.string(of: "errors[0].message")!)")
}
// A program can act on the code.
if jsonOut.string(of: "errors[0].code") == "capacityExceeded" {
print("The text is too long for the symbol size allowed.")
}
// Sample output:
// code: unknownOption
// path: /render/moduleSise
// message: unknown option
// code: outOfRange
// path: /encoding/qr/version
// message: the value is out of range
// code: capacityExceeded
// path: /payload
// message: the payload does not fit the requested QR version at the requested error-correction level
// The text is too long for the symbol size allowed.
}