NativeAdSlot
Displays a contextually targeted native ad inline within your SwiftUI view hierarchy. The slot auto-sizes to the creative height and collapses to zero when no ad is available.

Basic Usage
swift
import SimulaAdSDK
struct CharacterFeed: View {
let characters: [Character]
var body: some View {
ScrollView {
LazyVStack {
ForEach(Array(characters.enumerated()), id: \.offset) { index, character in
CharacterCard(character: character)
if index % 10 == 9 {
NativeAdSlot(
adUnitId: "SIM-NAT-XXXXXXXX",
onImpression: { data in
print("Impression: \(data.impressionId)")
}
)
}
}
}
}
}
}Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
adUnitId | String? | nil | Ad unit ID from the publisher dashboard. Used for measurement and improved targeting |
width | String? | nil | Width: number for dp, string with % or px suffix (e.g. "80%", "320px"). nil fills parent. Min 300 dp |
theme | String? | nil | "dark", "light", or "system". nil uses the SDK default |
preloadedAdId | String? | nil | ID from SimulaAds.preloadNativeAd() to render a cached ad instantly |
onImpression | (NativeAdData) -> Void | {} | Fires when the ad is >=50% visible for >=1 second |
onClick | () -> Void | {} | Fires when the user taps the ad CTA |
onPaid | (AdValue) -> Void | {} | Estimated per-impression revenue, co-timed with onImpression. See AdValue |
onError | (NativeAdError) -> Void | {} | Fires on load or render failure |
Preloading
Preload ads before they scroll into view for instant rendering:
swift
// Preload
let preloadedId = await SimulaAds.preloadNativeAd(
adUnitId: "SIM-NAT-XXXXXXXX",
theme: "system"
)
// Use in view
NativeAdSlot(
adUnitId: "SIM-NAT-XXXXXXXX",
preloadedAdId: preloadedId
)
// Clean up if the ad was never displayed
SimulaAds.destroyPreloadedAd(preloadedId)Preload limit
The SDK holds at most 5 preloaded native ads in memory. Excess ads are silently dropped.
Invalidation
Force-refresh a specific slot or clear all cached native ads:
swift
SimulaAds.invalidateNativeAd(adUnitId: "SIM-NAT-XXXXXXXX")
SimulaAds.invalidateNativeAds()