Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.7.0+

Generate a QR Code PNG File

See more QR Code Examples

Demonstrates the simplest way to create a QR code: the text to encode goes in payload.text, and output.path names the PNG file to write. QrCode chooses the smallest QR version that holds the text at error-correction level M, and the response reports what it chose and the size of the image. The request and the response are both JSON objects, and they must be two different objects.

Chilkat Objective-C Downloads

Objective-C
#import <CkoJsonObject.h>
#import <CkoFormats.h>

BOOL success = NO;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"payload.text" value: @"https://www.chilkatsoft.com/"];

//  Each module (one of the small squares) is drawn 8 pixels wide.  The default is 4.
[json UpdateInt: @"render.moduleSize" value: [NSNumber numberWithInt: 8]];

//  Write the image to a file.  An existing file is replaced only when output.overwrite is true.
//  Otherwise the call fails and the existing file is left as it was.
[json UpdateString: @"output.path" value: @"qa_output/qr_chilkat.png"];
[json UpdateBool: @"output.overwrite" value: YES];

//  The image is also returned in the response as base64 unless output.return is "none".
[json UpdateString: @"output.return" value: @"none"];

CkoFormats *fmt = [[CkoFormats alloc] init];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [fmt QrCode: @"generate" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@",fmt.LastErrorText);
    return;
}

NSLog(@"%@%@",@"file: ",[jsonOut StringOf: @"outputs[0].path"]);
NSLog(@"%@%d%@%d%@%d%@",@"image: ",[[jsonOut IntOf: @"outputs[0].width"] intValue],@" x ",[[jsonOut IntOf: @"outputs[0].height"] intValue],@" pixels, ",[[jsonOut IntOf: @"outputs[0].byteCount"] intValue]
    ,@" bytes");
NSLog(@"%@%d",@"QR version: ",[[jsonOut IntOf: @"symbols[0].selectedEncoding.version"] intValue]);
NSLog(@"%@%@",@"error correction: ",[jsonOut StringOf: @"symbols[0].selectedEncoding.errorCorrection"]);
NSLog(@"%@%d%@%d",@"modules: ",[[jsonOut IntOf: @"symbols[0].dimensions.modulesWide"] intValue],@" x ",[[jsonOut IntOf: @"symbols[0].dimensions.modulesHigh"] intValue]);

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

//  image

//  Sample output:

//  file: qa_output/qr_chilkat.png
//  image: 296 x 296 pixels, 1251 bytes
//  QR version: 3
//  error correction: M
//  modules: 29 x 29