This is the 5-minute integration walkthrough. Before you start, make sure you’ve completed Installation.
1. Create a session
The entry point is KontextAds.createSession(_:), which returns a Session you’ll use for the rest of the conversation lifecycle.
The session keeps userId, conversationId, and publisherToken fixed for its lifetime. Recreate the session when any of those change (e.g. when the user starts a new chat).
Session is @MainActor — call its methods from the main actor (default in SwiftUI views and @MainActor-isolated view controllers).
2. Feed conversation messages
Add every message to the session as it appears. User messages trigger a debounced preload in the background; assistant messages let the SDK link the matched ad to the corresponding placement.
addMessage returns synchronously. The preload result is delivered later via the onEvent callback (.filled, .noFill, .error, …) — not via a return value.
3. Render the ad
Use session.createAd(messageId:) to obtain an Ad for an assistant message, then render it with InlineAdUIView.
createAd is idempotent: calling it repeatedly with the same messageId returns the same Ad. Cache the returned instance so view-controller / cell reuse doesn’t recreate it.
4. Tear down
Call destroy() when the conversation ends or the view disappears. Idempotent and required to cancel pending network requests and release web view resources.
Observing events
Two equivalent ways to consume AdEvents — pick whichever fits your codebase.
onEvent callback
Combine publisher
Both deliver the same events on the main thread. The complete event list lives in the API reference.