Skip to main content

Loading several ads at once

To have several formats ready at once, ask for them and load them concurrently. Each format is requested and rendered in parallel, and one format without an ad leaves the others untouched:

Running your own auction

VideoAd carries the price before anything is rendered, so you can compare it with other networks and decide:
price.value is a CPM in price.currency. price.precision says whether it is a DSP’s clearing price (.exact) or a forecast for house demand (.estimated). reject tells the server at once, and the server tells the advertiser its win produced no impression. Use .outbid when another network won on price, with the winning price and the network, so we can optimize our bids for your traffic; .notUsed (the default) for any other reason: the moment passed, the viewer left. Rejecting is optional; an ad you never use is settled when it expires or when you call destroy().

Handling no-fill

format(_:) throws VideoAdError.noFill(reason:) when the server has nothing for the format. It is not a failure and the SDK never retries by itself; ask again at a later moment.
All of them mean the same for your code: no ad this time, ask again at the next moment. Log the reason if you want to see why fill is low.

Giving the SDK your player

Every request and event carries where the viewer is in the episode, and an overlay follows the playback: it shows over playing content only and goes away when the viewer pauses. Give the SDK your player once the episode starts playing. With an AVPlayer, hand it over; the SDK reads its time and watches its playback:
With any other player, conform it to VideoAdsPlayer: its position in seconds, and whether it plays. Buffering and seeking count as playing.
If your player already publishes its state, return it as isPlayingPublisher and an overlay ends the moment the viewer pauses; otherwise the SDK checks isPlaying four times a second while an overlay is on screen. The SDK holds the player weakly and lets go of it at destroy(). Call track(_:) when this episode starts playing, not when you create the VideoAds: until then the player’s time belongs to the previous episode. Before that the SDK sends 0, which is right for a prefetch.

Keeping the ad clear of your controls

An overlay strip sits at the bottom or top of the player view, a pause card at the top; the SDK keeps both inside the view’s safe area. If your player draws its own controls there (a title, a progress bar, buttons), pass the space they take as insets:
  • The insets add to the safe area, as UIKit’s additionalSafeAreaInsets do: measure from the safe area’s edge, not from the screen’s.
  • They stay while the ad is shown, through resizes and rotations. If your controls change size while an ad is on screen, the ad keeps the insets it was shown with.
  • The ad still has to fit at half its size or more. Insets count against the space, so containerTooSmall(minimum:) includes them.
  • A full-screen pause ad ignores them: it fills the view, and its own controls keep clear of the edges.

The next episode

Create the next episode’s VideoAds while the current one plays, prefetch its ads, and destroy the current one when the viewer leaves. Any number can be alive at once; the SDK keeps at most one ad on screen in the whole app.
Call track(_:) when the episode actually starts in the player, not at creation: before that the player’s time belongs to the previous episode.

Who the viewer is

Set what you know on the shared instance; the next request carries it:
Consent is read for you: on every request the SDK reads the IAB keys your consent platform writes to UserDefaults. Only when the SDK cannot detect a signal, because your app has no such platform or keeps a signal elsewhere, set it yourself. A value your consent platform wrote still wins.
See TCF & CMP.

Using it from SwiftUI

The SDK presents ads with UIKit, which SwiftUI apps use as well. A linear ad needs nothing extra: show() presents it over whatever SwiftUI is showing. An overlay or pause ad goes into a UIView, which show(in:) takes and SwiftUI’s VideoPlayer does not expose. Give it a transparent view laid over the player:
To keep the ad above a title or controls you draw over the player, pass their height as insets, as the example app does.
  • Create the view once and keep it on your model, not inside makeUIView: SwiftUI may rebuild the representable, and the ad would go with it.
  • Keep the VideoAds on your model too (held by @StateObject), not in the view struct: SwiftUI recreates view structs, and releasing the VideoAds ends its ads.
  • hitTest passes taps outside the ad through, so your own gestures on the player keep working.
  • The delegate is held weakly: a @StateObject model keeps it alive for the screen.
  • A linear ad covers your view, and SwiftUI calls onDisappear for that. Call ads.destroy() when the viewer actually leaves (your back button, the next episode), not in onDisappear.
The example app is a complete SwiftUI integration of all three formats.