iOS SDK
Quickstart
Configure Cartesian, sign the shopper in with a session token and open the assistant.
Configure at launch
Call Cartesian.configure once, in your App initializer or application(_:didFinishLaunchingWithOptions:). The base URL is your Cartesian backend.
import CartesianUI
import SwiftUI
@main
struct ShopApp: App {
private let shop = ShopDelegate()
init() {
Cartesian.configure(
CartesianConfig(
vertical: .grocery,
baseURL: URL(string: "https://cartesian.yourcompany.com")!,
userAgent: "FreshMart/3.2",
licenseKey: nil
),
options: CartesianOptions(
title: "FreshMart Assistant",
features: CartesianFeatures(cart: .host, wishlist: .off)
)
)
Cartesian.delegate = shop
}
var body: some Scene {
WindowGroup { HomeView() }
}
}Cartesian.delegate is a weak reference, so keep the delegate object alive yourself (here as a property of the app).
Sign in with a session token
Your backend issues a Cartesian session token for the signed-in shopper. Pass it to the SDK:
let user = try await Cartesian.client.signIn(sessionToken: token)swiftThe token is saved in the Keychain. On the next launch the client starts in .restoring and the UI restores the session automatically. Only a 401 from the backend signs the shopper out; network errors and 5xx responses keep the token and retry.
Grocery sends the token as the grocery_session cookie (REST and the WebSocket upgrade). Apparel sends Authorization: Bearer <token> and the WebSocket subprotocol bearer.<token>.
Open the assistant
struct HomeView: View {
@StateObject private var assistant = CartesianPanelState()
var body: some View {
ProductsGrid()
.overlay(alignment: .bottomTrailing) {
CartesianAskButton(state: assistant, label: "Ask FreshMart").padding(16)
}
.cartesianPanel(assistant)
}
}On iPhone this is a sheet that opens at full height; on iPad it docks on the right. See Open the assistant.
Receive cart events
With cart: .host, products the shopper adds go to your cart through the delegate:
@MainActor
final class ShopDelegate: CartesianDelegate {
func onAddToCart(_ product: Product, quantity: Int) {
MyCart.shared.add(id: product.id, quantity: quantity)
Cartesian.host.setCart(MyCart.shared.quantities)
}
func onOpenCart() { Router.shared.showCart() }
func onProductSelected(_ product: Product) { Router.shared.showProduct(product.id) }
}Cartesian.host.setCart keeps the counts on product cards and the header badge in sync with your cart.
Example app
sdk/ios/Example is a SwiftUI grocery app (FreshMart) with a home screen, Ask bar, Ask button and a host-owned cart. Copy Config.local.xcconfig.example to Config.local.xcconfig, fill in your backend host and a demo account, then run xcodegen generate.