Activate a licence in Swift
In this guide
In a macOS app, start activation from the licence form. URLSession exchanges JSON with Keyseal. CryptoKit verifies the certificate before the view model receives any entitlements.
To use the SDK, follow the client SDK integration guide.
Prepare the app
The example uses Foundation and CryptoKit. Store a stable fingerprint and licence state in Keychain or private application storage. Read HTTPURLResponse.statusCode before decoding the body. The functions below call the public API directly. Download SwiftLicence.swift.
Pin your organisation and product slugs (here atelier and dropsmith), the environment and its public keys in the build. The activation request sends the organisation and environment together. Obtain those keys at build time as described in the verification protocol. Keep management credentials and privileged calls on the publisher’s server.
Activate the first machine
Pass the key the buyer just typed and the local fingerprint to this function. Replace the sample machine name, platform, version and build date with your app’s values. A successful POST /v1/activate returns 200 with activation_id and certificate.
static func activate(key: String, fingerprint: String) async throws -> (Data, URLResponse) {
try await post("activate", [
"key": key, "product": product, "fingerprint": fingerprint,
"organization": "atelier", "environment": "test",
"machine_name": "Studio", "platform": "macos",
"app_version": "1.0.0", "build_date": "2026-11-17"
])
}
If every seat is taken (seats_exhausted), let the buyer release a machine. You can discard the entered key. Refresh sends activation_id, the original fingerprint and the current app_version.
Verify before granting access
Require two canonical base64url segments after keyseal:v1:. Read the kid from the UTF-8 JSON to select its pinned public key. Pass that key, the original payload segment as received and the 64 signature bytes to the function below. Use CryptoKit’s signing key type, Curve25519.Signing.PublicKey. Its initialiser takes the 32 raw bytes. A false result means bad_signature.
static func validSignature(publicKey: Data, segment: String, signature: Data) -> Bool {
guard let key = try? Curve25519.Signing.PublicKey(rawRepresentation: publicKey) else {
return false
}
return key.isValidSignature(signature, for: Data(segment.utf8))
}
The signature is one step in the complete verification procedure. Check every signed claim, the expected product and the fingerprint’s SHA-256. Match act to the ID in the response, then replace the stored ID and certificate together. Run the protocol’s 19 vectors, including modified signatures and retired keys.
Start offline, refresh and release
Verify the stored certificate on every launch. When the network is unavailable, an active lease allows use. At or beyond lease_until, open preserves the last verified entitlements and closed blocks use until a successful refresh. For a 1.x build, check its release date and major with claims.coversBuild(day, major: 1). A certificate with a signed update_major requires that exact major. If the claim is absent or null, historical date-based coverage still applies.
static func refresh(id: String, fingerprint: String) async throws -> (Data, URLResponse) {
try await post("refresh", [
"activation_id": id, "fingerprint": fingerprint, "app_version": "1.0.0"
])
}
static func deactivate(id: String, fingerprint: String) async throws -> (Data, URLResponse) {
try await post("deactivate", ["activation_id": id, "fingerprint": fingerprint])
}
Call POST /v1/refresh in the background once a day when online. Verify a new certificate before replacing the previous one. Immediately persist a blocked state on revoked, suspended or expired, retain the ID and continue refreshing. For released, unknown_activation or wrong_machine, clear the activation and offer to activate again. A network failure or 429 preserves the previous state according to its lease.
The “Release this machine” action calls POST /v1/deactivate. On 204, clear the certificate and ID without reading a JSON body. An unknown activation or one belonging to a different machine can also be forgotten. On network failure, leave the release pending.