Viewing is open to everyone. Downloading and starring need a GitHub sign-in.
Sign in with GitHub
README.md
v0.1.0-build.85
Latest
Commit ede38f5
Sign in with GitHub to download. Each download is recorded with your GitHub username.
| 1 | # CKey for Swift |
| 2 | |
| 3 | A thin Swift package over the CKey C++20 core (iOS 15+, macOS 12+). It encodes and renders CKey symbols as native pixels (`CGImage`, SwiftUI, UIKit, AppKit), exports PNG, SVG and PDF, scans still images and live camera frames, and classifies decoded payloads. All encoding, decoding and rendering is done by the core in `src/`. The Swift layer reimplements none of it. |
| 4 | |
| 5 | ## Install |
| 6 | |
| 7 | In Xcode, choose **File > Add Package Dependencies...** and enter the repository URL: |
| 8 | |
| 9 | ``` |
| 10 | https://github.com/clearkeysolutions/ckey |
| 11 | ``` |
| 12 | |
| 13 | Or add it to a `Package.swift`: |
| 14 | |
| 15 | ```swift |
| 16 | .package(url: "https://github.com/clearkeysolutions/ckey", from: "0.1.0"), |
| 17 | // ... |
| 18 | .target(name: "MyApp", dependencies: [.product(name: "CKey", package: "ckey")]), |
| 19 | ``` |
| 20 | |
| 21 | `Package.swift` sits at the repository root because SwiftPM installs packages from the root of a Git repository. The Swift sources live in `sdk/swift/`. |
| 22 | |
| 23 | ### Products |
| 24 | |
| 25 | | Product | Import | Contains | |
| 26 | |---|---|---| |
| 27 | | `CKey` | `import CKey` | Everything: encoding, the native renderer, views, scanning, payload helpers, style certification and the optional aggregate telemetry. `import CKey` also re-exports the scanning API. | |
| 28 | | `CKeyScanner` | `import CKeyScanKit` | Scanning only: `CKeyDecoder`, `CKeyCameraScanner`, `CKeyCameraPreview`, `CKeyPayload`. It links the codec and scan libraries and nothing else, so it has no renderer and no telemetry. Use it in OS or vendor builds that must leave telemetry out entirely. | |
| 29 | |
| 30 | Package layout: `CKeyCodec`, `CKeyScan`, `CKeyRender`, `CKeyCertify` and `CKeyTelemetry` are C++ targets. Each one compiles the matching file in `src/` in place, through a one-line shim under `sdk/swift/Sources/<Target>/`. `CKeyCodec` exposes `include/ckey/ckey.h` as a Clang module, and `CKeyTelemetry` does the same for `include/ckey/telemetry.h`. `CKeyScanKit` and `CKey` are the Swift API. |
| 31 | |
| 32 | ## Encode and render |
| 33 | |
| 34 | ```swift |
| 35 | import CKey |
| 36 | |
| 37 | let symbol = try CKeySymbol.encode("https://openckey.org") // version 0 = smallest that fits |
| 38 | let dense = try CKeySymbol.encode(data, version: 12, level: .h, layout: .print) |
| 39 | |
| 40 | let image: CGImage = try symbol.cgImage(size: 1024) // opaque, black on white |
| 41 | let overlay = try symbol.cgImage(size: 512, policy: .hybrid, transparent: true, style: .plain) |
| 42 | let png: Data = try symbol.pngData(size: 1024, dpi: 300) |
| 43 | let svg: String = try symbol.svg(scale: 8, style: .concept) |
| 44 | let pdf: Data = try symbol.pdfData(widthPoints: 144) // 2 inches wide |
| 45 | let raw = try symbol.pixels(size: 256) // straight-alpha RGBA8 buffer |
| 46 | ``` |
| 47 | |
| 48 | `size` is the pixel size of the full 31-unit canvas, which includes the quiet margin. The raster policies are `.coverage` (antialiased, the default), `.binary` and `.hybrid` (thin marks kept at least 1 px wide). `CKeySymbol.capacity(version:level:layout:)` gives the payload limit for a version. `CKeyError` explains any refusal. |
| 49 | |
| 50 | ### Styles |
| 51 | |
| 52 | `CKeyStyle.plain` is black on white, and `CKeyStyle.concept` is cream metallic ink on a dark vignette. Every field of the core's `ckey_style` can be changed: colours, accents, module shape, ring style, ray caps, bevel, shadow, texture and logo. |
| 53 | |
| 54 | ```swift |
| 55 | var style = CKeyStyle.concept |
| 56 | style.modules = .circle |
| 57 | style.policy = .adaptive // .strict (default) refuses unsafe styles; .override draws as given |
| 58 | try symbol.check(style: style) // explains a refusal (contrast, effects, logo budget) |
| 59 | let report = try symbol.certify(style: style, transparent: true, backdrops: [.white, CKeyColor(hex: 0x202020)]) |
| 60 | print(report.certified, report.message, report.adaptedStyle as Any) |
| 61 | ``` |
| 62 | |
| 63 | Certification renders the symbol, composites it over the backdrops, applies a phone-camera model and decodes it with the real decoder. A transparent symbol with no backdrop is never certified. The model is a model and not a measurement, so real-camera tests remain the evidence. |
| 64 | |
| 65 | ## Views |
| 66 | |
| 67 | ```swift |
| 68 | import SwiftUI |
| 69 | import CKey |
| 70 | |
| 71 | struct TicketView: View { |
| 72 | var body: some View { |
| 73 | CKeyView(data: "ABC123") |
| 74 | .frame(width: 180, height: 180) |
| 75 | .background(Color.white) // transparent by default: pick a contrasting backdrop |
| 76 | } |
| 77 | } |
| 78 | ``` |
| 79 | |
| 80 | `CKeyView` is square. It renders at its exact pixel size (points × display scale) through `CGImage`, so no SVG, file or web view is involved. It draws no background, border or chrome unless you pass `transparent: false`. It is one accessibility image with the label "CKey code"; change it with `accessibilityLabel:`. Other initialisers are `CKeyView(bytes:)` and `CKeyView(symbol:style:...)`. |
| 81 | |
| 82 | UIKit and AppKit have `CKeyUIView` and `CKeyNSView`: set `symbol`, `style`, `transparent` and `policy`, or use `init(data:)`. |
| 83 | |
| 84 | ## Scan |
| 85 | |
| 86 | Headless, from any thread: |
| 87 | |
| 88 | ```swift |
| 89 | let result: CKeyResult? = CKeyDecoder.decode(cgImage: image) |
| 90 | let fromCamera = CKeyDecoder.decode(pixelBuffer: pixelBuffer) // 420v/420f luma, gray8, BGRA/RGBA/ARGB |
| 91 | if let result { |
| 92 | result.bytes // Data, verified by Reed-Solomon and CRC-32C |
| 93 | result.text // String? — UTF-8 without control characters (tab/LF/CR allowed) |
| 94 | result.profile // .scalable, .locator, .text, .extended |
| 95 | result.version // Profile 0 version 1...31 (0 otherwise) |
| 96 | result.layout // .standard / .print for Profile 0 |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | The pipeline is the iOS reader's (`ios/CKeyReader/CameraScanner.swift`). It finds up to six border candidates in the whole frame. It fits each one to the Profile 0 field and decodes it, then tries Profiles 1, 2 and 3, each refined against its own keys. Images are drawn over white, so a transparent rendering decodes as dark on white. |
| 101 | |
| 102 | Live camera with `CKeyScanner` (in `CKey`, with telemetry sessions) or `CKeyCameraScanner(observer: nil)` (in `CKeyScanner`, without telemetry): |
| 103 | |
| 104 | ```swift |
| 105 | let scanner = CKeyScanner() |
| 106 | scanner.onResult = { result in // main queue, first valid decode of the session |
| 107 | show(result) // then call scanner.scanAgain() for the next one |
| 108 | } |
| 109 | scanner.start() // asks for camera access (NSCameraUsageDescription) |
| 110 | // SwiftUI: CKeyCameraPreview(session: scanner.session) |
| 111 | scanner.stop() |
| 112 | ``` |
| 113 | |
| 114 | Frames are processed off the main thread at up to 20 per second. Late frames are discarded, and no still capture is needed. Call `start()`, `stop()` and `scanAgain()` from the main thread. |
| 115 | |
| 116 | ### Payload actions |
| 117 | |
| 118 | ```swift |
| 119 | if let text = result.text, let action = CKeyPayload.classify(text) { |
| 120 | switch action { |
| 121 | case .web(let url): showButton("Open \(url.host ?? "link")") |
| 122 | case .email(_, let address): showButton("Email \(address)") |
| 123 | case .call(_, let number): showButton("Call \(number)") |
| 124 | case .message(_, let number): showButton("Message \(number)") |
| 125 | case .geo(_, _, let label, let mapURL): showButton("Show \(label ?? "location") in Maps", mapURL) |
| 126 | case .address(let address, let mapURL): showButton("Show \(address) in Maps", mapURL) |
| 127 | case .contact(let vCard, let name): showButton("Add \(name) to Contacts…", vCard) |
| 128 | } |
| 129 | } |
| 130 | ``` |
| 131 | |
| 132 | `classify` follows `spec/CKey-Payload-Conventions.md`. It never acts on a payload. Only the allowlisted forms get an action, and each one is validated first. Everything else returns `nil` and is shown as plain text, including `javascript:`, `data:`, `file:`, `intent:` and custom schemes. Act only when the user taps, and keep the payload visible. |
| 133 | |
| 134 | ## Safe Actions |
| 135 | |
| 136 | A Safe Action (`ckey-action:1:…`) is a signed code from a known site for this app. `CKeySafeActions` verifies it with the core and decides whether it runs; the app only pins keys and registers handlers. |
| 137 | |
| 138 | ```swift |
| 139 | let actions = CKeySafeActions(appID: "com.viadere") |
| 140 | .trust(issuer: "citeory.com", keyID: "2026-11", publicKeyHex: "<64 hex digits>", |
| 141 | requestInfo: "https://citeory.com/api/ckey/requests/{request_id}") |
| 142 | .register("open.library", issuers: ["citeory.com"]) { action in openItem(action.paramsText) } |
| 143 | .register("signin.request", issuers: ["citeory.com"], scope: .crossDevice) { action in approveSignIn(action) } |
| 144 | |
| 145 | switch await actions.handle(text) { |
| 146 | case .notASafeAction: showPayload(text) // the ordinary payload rules |
| 147 | case .performed: break // same-device: already ran |
| 148 | case .needsConfirmation(let request): pending = request // present CKeySafeActionPrompt(request: request) |
| 149 | case .rejected(let reason): showUnverified(reason) |
| 150 | } |
| 151 | ``` |
| 152 | |
| 153 | Same-device actions run at once. Cross-device actions run only from `CKeySafeActionPrompt`, which shows the verified issuer and who is asking; there is no other way to approve one. Anything that fails a check runs nothing; show that the code claims to be a Safe Action but could not be verified, with the reason. See [spec/CKey-Safe-Actions.md](../../spec/CKey-Safe-Actions.md). |
| 154 | |
| 155 | ## Telemetry |
| 156 | |
| 157 | The `CKey` product keeps aggregate scan counters by default (mode 1) and sends them to `https://openckey.org/api/telemetry/v1/batch` at most once an hour. The normative rules are in [docs/adoption-and-telemetry.md](../../docs/adoption-and-telemetry.md). |
| 158 | |
| 159 | **Opt out in one line**, at launch and before scanning: |
| 160 | |
| 161 | ```swift |
| 162 | CKey.telemetry = .off // or: CKey.telemetry = 0 |
| 163 | ``` |
| 164 | |
| 165 | | Mode | Swift | Counts locally | Sends | |
| 166 | |---|---|---|---| |
| 167 | | 0 | `.off` | no (switching purges every counter) | no | |
| 168 | | 1 | `.on` (default) | yes | aggregate batches, at most hourly | |
| 169 | | 2 | `.local` | yes | never | |
| 170 | |
| 171 | - `CKey.telemetry` is a `CKeyTelemetryMode`. Integer literals `0`, `1` and `2` also work (`CKey.telemetry = 2`). Any other literal is a programming error that stops the program; it is never treated as 1. |
| 172 | - For values known only at run time, such as those from a configuration file, use `try CKey.setTelemetry(rawValue: n)`. It throws `CKeyError` with code `.invalidConfiguration` for anything other than 0, 1 or 2, and leaves the mode unchanged. `CKeyTelemetryMode(rawValue:)` is failable. |
| 173 | - `CKey.telemetryCounters` returns the local totals (modes 1 and 2). |
| 174 | - The mode is process-wide and is not persisted. |
| 175 | |
| 176 | **What is counted:** |
| 177 | - scan sessions: started, succeeded and failed; |
| 178 | - processed frames; |
| 179 | - decodes per profile; |
| 180 | - time-to-first-decode and per-frame histograms; |
| 181 | - the SDK name (`swift`) and version, the platform (`ios` or `macos`) and the UTC day. |
| 182 | |
| 183 | **What is never collected:** |
| 184 | - payloads or URLs; |
| 185 | - user, device, installation or advertising IDs; |
| 186 | - location; |
| 187 | - per-scan histories. |
| 188 | |
| 189 | **How sessions are recorded:** a session begins at `start()` and at `scanAgain()`, succeeds at its first valid decode, and fails at `stop()` or after 30 s without a decode. If you run your own camera pipeline with `CKeyDecoder`, report its sessions through `CKeyTelemetrySession.shared`. |
| 190 | |
| 191 | **How batches are sent:** |
| 192 | - The core decides when a batch is due. The SDK checks every 15 minutes, after each session ends and when the app moves to the background (`UIApplication.didEnterBackgroundNotification` on iOS, `NSApplication.didResignActiveNotification` on macOS). |
| 193 | - Each batch is one HTTPS POST through an ephemeral, cookie-less `URLSession`, made on a private utility queue and never on the scan queue. |
| 194 | - On iOS, the background attempt runs under `ProcessInfo.performExpiringActivity`, so it also works in app extensions. |
| 195 | - A failed send keeps the batch for a later attempt. |
| 196 | |
| 197 | **Privacy manifest:** the `CKey` product ships a `PrivacyInfo.xcprivacy` in its resource bundle. It declares: |
| 198 | - Product Interaction, used for Analytics, not linked to the user and not used for tracking; |
| 199 | - no tracking domains; |
| 200 | - a monotonic clock used to measure elapsed time between events (reason `35F9.1`). |
| 201 | |
| 202 | Integrators still make their own App Store disclosures. Apps that must not contain telemetry at all should use the `CKeyScanner` product instead. |
| 203 | |
| 204 | ## Develop |
| 205 | |
| 206 | ```sh |
| 207 | swift build |
| 208 | swift test |
| 209 | ``` |
| 210 | |
| 211 | The tests (XCTest) cover: |
| 212 | - encode → `CGImage` → decode round trips for every raster policy and both layouts, including the `CVPixelBuffer` path; |
| 213 | - PNG, PDF and SVG output; |
| 214 | - transparent alpha; |
| 215 | - style refusal and override; |
| 216 | - certification; |
| 217 | - telemetry mode validation and local counting; |
| 218 | - payload classification. |
| 219 | |
| 220 | CI runs `.github/workflows/sdk-swift.yml` on `macos-14`. |