Objective-C Requires Chilkat v11.7.0+
Objective-C
Choose the QR Code Version and Mask Pattern
See more QR Code Examples
Demonstrates fixing the QR version and the mask pattern. The version (1 to 40) is the size of the symbol: version 1 is 21 modules wide and each version adds 4. By default QrCode picks the smallest version that holds the data and the mask pattern that scores best under the QR standard. Fixing the version is useful when a series of codes must all be the same size, and minVersion and maxVersion bound the automatic choice instead.Chilkat Objective-C Downloads
#import <CkoFormats.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoFormats *fmt = [[CkoFormats alloc] init];
CkoJsonObject *json = [[CkoJsonObject alloc] init];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
[json UpdateString: @"payload.text" value: @"https://www.chilkatsoft.com/"];
// Automatic: the smallest version that holds the text, and the best-scoring mask.
success = [fmt QrCode: @"generate" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",fmt.LastErrorText);
return;
}
NSLog(@"%@%d%@%d%@%d",@"automatic: version ",[[jsonOut IntOf: @"symbols[0].selectedEncoding.version"] intValue],@", ",[[jsonOut IntOf: @"symbols[0].dimensions.modulesWide"] intValue]
,@" modules wide, mask ",[[jsonOut IntOf: @"symbols[0].selectedEncoding.mask"] intValue]);
// A fixed version. The call fails with capacityExceeded if the text does not fit it.
[json UpdateInt: @"encoding.qr.version" value: [NSNumber numberWithInt: 8]];
success = [fmt QrCode: @"generate" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",fmt.LastErrorText);
return;
}
NSLog(@"%@%d%@%d%@%d",@"version 8: version ",[[jsonOut IntOf: @"symbols[0].selectedEncoding.version"] intValue],@", ",[[jsonOut IntOf: @"symbols[0].dimensions.modulesWide"] intValue]
,@" modules wide, mask ",[[jsonOut IntOf: @"symbols[0].selectedEncoding.mask"] intValue]);
// A fixed mask pattern, 0 to 7. Every mask gives a valid symbol holding the same data.
[json UpdateInt: @"encoding.qr.mask" value: [NSNumber numberWithInt: 5]];
success = [fmt QrCode: @"generate" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",fmt.LastErrorText);
return;
}
NSLog(@"%@%d%@%d",@"version 8, mask 5: version ",[[jsonOut IntOf: @"symbols[0].selectedEncoding.version"] intValue],@", mask ",[[jsonOut IntOf: @"symbols[0].selectedEncoding.mask"] intValue]);
// Bounds for the automatic choice: no smaller than version 4, no larger than version 10.
CkoJsonObject *json2 = [[CkoJsonObject alloc] init];
[json2 UpdateString: @"payload.text" value: @"https://www.chilkatsoft.com/"];
[json2 UpdateInt: @"encoding.qr.minVersion" value: [NSNumber numberWithInt: 4]];
[json2 UpdateInt: @"encoding.qr.maxVersion" value: [NSNumber numberWithInt: 10]];
success = [fmt QrCode: @"generate" jsonIn: json2 jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",fmt.LastErrorText);
return;
}
NSLog(@"%@%d",@"minVersion 4: version ",[[jsonOut IntOf: @"symbols[0].selectedEncoding.version"] intValue]);
// The four symbols, in the order they were made. The second and the third hold the same data
// in the same version and differ only in the mask:
//
// Sample output:
// automatic: version 3, 29 modules wide, mask 2
// version 8: version 8, 49 modules wide, mask 2
// version 8, mask 5: version 8, mask 5
// minVersion 4: version 4