Skip to main content

Installation

Add the package in Xcode via File → Add Package Dependencies:
Or in your Package.swift:

How the pieces fit together

1

Initialize the client

Create a Gravity instance with your API key and production: true.
2

Collect context & fetch ads

Call gravity.getAds(...) with the conversation messages, session ID, and placements. The SDK auto-collects device signals (UA, OS, screen, timezone). Run it in parallel with your LLM call.
3

Render

Use the built-in GravityAdView (SwiftUI) or render the ad data yourself. Impression tracking fires automatically on appear.

Quick start

Constructor

getAds()

The SDK auto-collects device signals (User-Agent, OS, screen size, timezone, locale, device type) and sends them with the request. For mobile apps, this means your dashboard’s Device and Geography breakdowns work automatically — no manual device forwarding needed.
getAds() never throws. On any failure, it returns AdResult.empty (empty ads array). Safe to fire-and-forget.

Placements

SwiftUI — render ads

SpecAdView automatically handles the Gravity-managed placement design, safe fallback, impression and click tracking, required ad disclosure, and server-requested feedback controls.

Custom rendering

If you render ads with your own UI, fire tracking manually:

PII hashing

Hash email and phone for attribution without sending raw values:

Ad feedback

Full example — parallel with LLM stream

FAQ

No. Use async let to fire getAds() in parallel with your LLM call. It never throws, so failures are silent.
No. The SDK does not access the Advertising Identifier. If you have IDFA consent via ATT, pass it as extraDeviceFields: ["ifa": idfa].
iOS 15+, macOS 12+, tvOS 15+, watchOS 8+.
No. It uses only Foundation, CryptoKit, and URLSession — all built into the platform.