Initialize
Start the SDK once at launch, in App.init or application(_:didFinishLaunchingWithOptions:), with a publishable key.
Call initialize at launch
Call Lander.initialize once, as early as possible. It returns immediately.
import Lander
import SwiftUI
@main
struct AcmeShopApp: App {
init() {
do {
try Lander.initialize(
apiKey: "pk_live_...", // publishable key from Settings > API keys
enableLogging: isDebug
)
} catch {
assertionFailure("Lander: \(error)") // a bad key; never a network error
}
}
var body: some Scene {
WindowGroup { ContentView() }
}
}
#if DEBUG
let isDebug = true
#else
let isDebug = false
#endifinitialize is synchronous and does no network work on the calling thread. Loading (or creating) the install ID and the launch report to Lander (POST /v1/sdk/init) run in the background, so a slow or missing network never delays your first frame.
Parameters
static func initialize(
apiKey: String,
baseURL: URL = Lander.defaultBaseURL, // https://api.golander.link
enableLogging: Bool = false
) throwsProp
Type
Use a publishable key
Create keys in the dashboard under Settings > API keys (Dashboard: API keys). Only publishable keys are accepted:
| Key | In an app? |
|---|---|
pk_test_... | Yes. Test environment. |
pk_live_... | Yes. Live environment. |
sk_test_..., sk_live_... | Never. initialize throws a LanderError with code invalid_argument. |
Anything you ship in an app binary is public, which is why secret keys are refused outright. Any other value that is not a publishable key (pk_test_ or pk_live_ followed by at least 24 letters or digits) also throws invalid_argument. Surrounding whitespace is ignored, and the error never contains the key, so it is safe to log.
Test and live builds
The key you pass decides which environment the app talks to. Links created in the test environment only resolve for apps using a pk_test_ key, and live links only for pk_live_ keys. See Environments.
A simple way to switch is a build setting per configuration. Add a user defined build setting LANDER_KEY with a pk_test_ value for Debug and a pk_live_ value for Release, expose it in Info.plist as LanderKey = $(LANDER_KEY), and read it at launch:
let key = Bundle.main.object(forInfoDictionaryKey: "LanderKey") as? String ?? ""
try? Lander.initialize(apiKey: key)If the setting is missing, the key is an empty string and initialize throws, so the mistake shows up on the first run instead of in production.
If you switch an existing install from a test key to a live key (or to another project), Lander reports that the install ID belongs elsewhere (install_id_conflict). The SDK then creates a fresh install ID and reports a first launch, so nothing breaks.
What initialize does
- Validates the key (throws
invalid_argumentfor secret or malformed keys). - Starts the SDK in the background and returns.
- Loads the install ID from storage, or creates a random UUID v4 on first launch.
- Starts taking the links you pass to
Lander.handle(url:)(including any you passed beforeinitialize) and watching the app lifecycle. - Reports the launch with
POST /v1/sdk/init. On a first launch the answer may carry a deferred link. - Loads any events left in the on device queue from earlier sessions and sends them.
initialize never throws because of the network. A failed launch report is logged (when logging is on). A failed first launch report is retried in the same session (when the app returns to the foreground, before claimHandoff and before events are sent) and on the next launch. The install is counted once, because the API keeps one record per install ID.
Rules worth knowing
- Call it once. A second call is ignored (it logs a message when logging is on).
- Some methods need it.
createLinkandclaimHandoffthrow aLanderErrorwith codenot_initializedbeforeinitialize.initialLink()returnsniland logs a warning. - Some do not.
Lander.handle(url:),Lander.deepLinks,Lander.onDeepLinkandLander.trackEventwork beforeinitialize: links and events are kept and processed once the SDK starts. installIDisniluntil the SDK has started, which happens a moment afterinitializereturns.
The install ID
Lander.installID is the random UUID v4 the SDK created for this install. It is not tied to the device or the user:
- It is stored in a file in Application Support, in a directory excluded from iCloud and device backups, so it is never restored onto another device.
- It is gone when the app is uninstalled. A reinstall gets a new one.
It is handy for support requests: show it in a debug screen so testers can quote it.
Text("Install ID: \(Lander.installID ?? "(loading)")")Next steps
Universal Links setup
Claim your Lander domain with Associated Domains, let Lander host the apple-app-site-association file, and add an optional custom URL scheme.
Receiving links
Hand URLs to Lander, listen to Lander.deepLinks, understand cold start, warm start and deferred delivery, and route in SwiftUI or UIKit.