Zum Inhalt springen

Die Entwickler-Doku gibt es nur auf Englisch.

Swift SDK

The Swift SDK is a Swift package over the C interface of the Rust core, for apps on iOS 15 and macOS 12 or later. It has the same calls, states and error codes as every SDK and speaks protocol version 1. Source: sdks/swift/.

Pre-release, partly untested. Versions 0.x are test versions and not meant for production; LicenseClient.isPrerelease is true for them. The package name LicenseSDK is provisional. The calls are tested on Linux against the protocol test vectors. The Keychain store, the Secure Enclave chip, App Attest and the XCFramework build have not run on a Mac or an iPhone yet: check them before you rely on them.

Store apps sign the end customer in with an account instead of asking for a key (signing in): Apple's rules do not allow unlocking features with a key typed into the app.

Get it

The package is not published yet. Build the native library as an XCFramework on a Mac with Xcode and the Rust targets for iOS and macOS, then add sdks/swift as a local package in Xcode:

rustup target add aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-ios aarch64-apple-darwin x86_64-apple-darwin
sdks/swift/build-xcframework.sh     # writes sdks/swift/LicenseSDKNative.xcframework

Package.swift links that XCFramework when it is there. Without it, as on Linux, it links liblicense_sdk from the library search path.

Quickstart

import LicenseSDK

let client = try LicenseClient(.apple(
    productId: "prod_01J...",
    environment: .live,
    apiBase: "https://...",          // your Client API base URL
    appVersion: "1.4.0",
    publicKeys: ["k4.public...."]    // from the dashboard
))

// At every start: check locally, no network needed.
do {
    let status = try client.check()
    unlock(pro: status.has("pro"), seats: status.limit("seats"))
} catch let e as LicenseError where e.code == "not_activated" {
    showSignIn()
}

LicenseConfiguration.apple(...) keeps the SDK's state in the Keychain and, on iOS, binds the activation to a key in the Secure Enclave. LicenseConfiguration(...) takes the same settings plus your own store, transport, chip, appAttest and configSchema. For products with App Attest pass appAttest: true (App Attest).

Every call with network blocks until the answer or the transport's timeout. Never call it on the main thread:

let status = try await Task.detached { try client.validate() }.value

start() checks locally and renews in the background:

let start = client.start()
if let status = start.status { use(status) }
if let renewal = start.renewal {
    Task.detached {
        do { use(try renewal.wait()) }
        catch let e as LicenseError where e.isTransient { /* offline: keep the local result */ }
    }
}

Signing in instead of a key

let account: LicenseAccount
do {
    account = try client.signIn(email: email, password: password)
} catch let e as LicenseError where e.code == "totp_required" {
    account = try client.signIn(email: email, password: password, totpCode: askForCode())
}
let status = try client.activate(account: account)

activate(userToken:) activates with a token your own backend signed; activate(key:) with a license key, for apps outside the App Store.

Example app

sdks/swift/Examples/SignIn.swiftpm is a small SwiftUI app for iOS 15 that does all of the above: the local check at start, the renewal behind it, sign-in with a second factor, activate(account:), what the plan unlocks, and sign-out with deactivate(). It never asks for a key. Build the XCFramework first, enter your product's settings in Sources/Settings.swift and open the folder in Xcode or Swift Playgrounds. Its texts are in English and German. It has not run on an iPhone yet; on Linux only its model is type-checked.

Calls

Swift What it does
check() Checks the stored license locally
activate(key:), activate(userToken:), activate(account:) Activates this device
signIn(email:password:totpCode:) Signs the customer in, gives a LicenseAccount
validate() A fresh license token from the server
heartbeat() Tells the server the app runs; returns the seconds until the next one
deactivate() Frees this device's seat
start() Local check now, renewal in the background
remoteConfig(), cachedRemoteConfig() Remote config, checked against configSchema
reportUsage(entitlement:amount:reportId:) Counts units of a quota
offlineRequest(), importOffline(_:) Activation without network

LicenseStatus has state (.fresh or .grace), expiresAt, offlineUntil, licenseExpiresAt, licenseGraceUntil, licenseId, activationId, channel, has(_:), limit(_:), variant(_:) and claims.

Updates, downloads, assets, proxy calls, remote actions, crash reports and modules are not in the Swift layer yet. On iOS the App Store delivers updates; on macOS use the C interface for the rest.

Errors

Every failure is a LicenseError. Decide on code, the same code as in every SDK (full list in protocol section 7); kind groups them, isTransient is true when no usable answer arrived (a stored license stays usable), and retryAfter says how long to wait after rate_limited. The message is English text for logs, never for end users.

Keychain and Secure Enclave

  • KeychainStore: the SDK's state as generic passwords of service <productId>.<test|live>, kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly: readable by the background renewal once the device was unlocked after a restart, never synced to iCloud and never carried to another device by a backup. A device restored from a backup activates again instead of carrying the old device's license. Pass accessGroup to share the state with your app extensions.
  • SecureEnclaveChip: one P-256 key per product in the Secure Enclave, through CryptoKit, which never leaves it. The device ID is the SHA-256 of its public key, and every signed call carries its signature, so a copied activation does not work on another device (security chip). The Keychain keeps only the key's encrypted handle. Without a Secure Enclave (the Simulator) the activation binds by installation ID. On macOS the Rust core finds the Secure Enclave on its own.

App Attest

When you set up App Attest for a product (PATCH /v1/products/{id}/attestation), the server requires it for every iOS activation. Turn it on in the app as well:

let client = try LicenseClient(.apple(
    productId: "prod_01J...",
    apiBase: "https://...",
    appVersion: "1.4.0",
    publicKeys: ["k4.public...."],
    appAttest: true
))
  • AppAttestService (iOS 14 or later) takes the place of SecureEnclaveChip: one App Attest key per product through DCAppAttestService, its key ID in the Keychain. The key ID is the device ID. The activation carries the key's attestation, every later call an assertion over the request, as protocol section 6.7 ("Chip key") describes. Apple's servers vouch for both, so a copied activation or a modified app does not work.
  • The app needs the App Attest capability (entitlement com.apple.developer.devicecheck.appattest-environment). Keys of an app signed for development are accepted in the test environment only.
  • Apple attests a key only once. After deactivate(), or when Apple refuses a key, the next activation starts a new one.
  • Where App Attest is not supported, as in the Simulator, there is no key: the server then refuses iOS activations of the product. Try the Simulator with a product without App Attest.
  • On macOS appAttest changes nothing.

Your own storage, transport and chip are the protocols LicenseStore, LicenseTransport and LicenseChip, and LicenseAppAttest for App Attest; their calls may come from any thread, never two at once for one client.

Security properties

As for the Rust SDK: public keys only, no secret in the app, no plain http:// except for localhost, no code from the server is ever run. The Swift layer adds no network of its own.

Tests

sdks/swift/test.sh builds the native library with the test hooks and runs every license token, activation answer and account answer of the test vectors through the Swift calls, plus the store, transport, chip and App Attest callbacks. It runs on Linux and macOS with Swift 5.9 or later. On Linux it leaves out the Keychain, the Secure Enclave and App Attest, which only exist on Apple platforms.