Interstitial ad (SwiftUI)
Interstitial advertising is a full-screen ad format embedded within the app content during natural pauses, such as transitioning between game levels or completing a target action.
When an app shows an interstitial ad, the user has a choice: either tap the ad and go to the advertiser's website, or close the ad and return to the app.
Interstitial ads capture the user's full attention on the ad, which is why their CPM is higher.
Appearance
This guide shows how to integrate interstitial ads into an iOS app using SwiftUI. In addition to code examples and instructions, it provides recommendations on using this ad format and links to additional resources.
Prerequisite
- Follow the SDK integration steps described in Quick start.
- Initialize your ad SDK in advance.
- Make sure you're running the latest Yandex Mobile Ads SDK version. If you're using mediation, make sure you're also running the latest version of the unified build.
Implementation
Key steps for integrating interstitial ads in SwiftUI:
- Attach the
.interstitialAd(isPresented:request:onEvent:)modifier to the root (or relevant)View. - Control loading via
Binding<AdRequest?>: a non-nil value starts loading. - Control display via
Binding<Bool>: a value oftrueshows the already-loaded ad (if loading is still in progress, display will be deferred until ready). - Pass additional settings if you are working via Adfox (via
AdRequestparameters). - Handle the lifecycle in the
onEventclosure (InterstitialAdEvent).
Interstitial ad integration notes
-
Attempting to load a new ad upon receiving an error in the
.didFailToLoadevent is strongly discouraged. If you need to retry loading from the error handler, limit the number of reload attempts to avoid continuous failed ad requests in case of network connection restrictions. -
Keep the modifier on a view that lives throughout the display scenario (for example, on a screen or navigation root) to avoid losing the load state.
Loading and showing ads
For interstitial ads in SwiftUI, use AdRequest with the ad unit ID and two states: an optional request and a display flag.
To receive notifications about successful or failed loading, display, dismissal, and errors, use the onEvent closure with the InterstitialAdEvent enum.
import SwiftUI
import YandexMobileAds
struct InterstitialView: View {
@State private var adRequest: AdRequest?
@State private var isPresented = false
var body: some View {
VStack {
Button("Load and show") {
adRequest = AdRequest(adUnitID: "R-M-XXXXX-YY")
}
}
.interstitialAd(isPresented: $isPresented, request: $adRequest) { event in
switch event {
case .didLoad:
isPresented = true
case .didDismiss, .didFailToShow, .didFailToLoad:
adRequest = nil
default:
break
}
}
}
}
The ad unit ID (adUnitId) is obtained from the Yandex Advertising Network interface.
You can extend the ad request parameters via AdRequest by passing user interest data, page context data, location, or other additional data in the initializer. Additional contextual data in the request can significantly improve ad quality. For more information, see Ad targeting.
Example: separately initiate loading, then show the ad when ready:
struct InterstitialView: View {
@State private var adRequest: AdRequest?
@State private var isPresented = false
func loadAd() {
adRequest = AdRequest(adUnitID: "R-M-XXXXX-YY")
}
func showAd() {
isPresented = true
}
var body: some View {
VStack {
Button("Load", action: loadAd)
Button("Show", action: showAd)
}
.interstitialAd(isPresented: $isPresented, request: $adRequest) { event in
if case .didLoad = event {
// You can call showAd() or set isPresented = true
}
}
}
}
If isPresented = true is set before loading completes, the display will be performed automatically after a successful load.
Showing the ad
Interstitial ads should be displayed during natural pauses in app usage. A good example is between game levels or after completing a target action, such as after a file download completes.
After a successful load, the .didLoad event arrives — set isPresented = true to show the ad (or set the display flag in advance, see above).
The isPresented flag is reset to false after the ad is dismissed, a show error occurs, or a load error occurs.
Testing interstitial ad integration
Using demo blocks for ad testing
We recommend using test ads to test your ad integration and your app itself.
To guarantee that test ads are returned for every ad request, we created a special demo ad placement ID. Use it to check your ad integration.
Demo adUnitId: demo-interstitial-yandex.
Warning
Make sure that before placing the application in the store, you replaced the demo ad placement ID with a real one, obtained in the Yandex Advertising Network interface.
You can find the list of available demo ad placement IDs in the Demo ad units for testing section.
Verifying correct ad integration
You can test your ad integration using the native Console tool.
To view detailed logs, call the YMAMobileAds class's enableLogging method.
YMAMobileAds.enableLogging()
To view SDK logs, go to the Console tool and set Subsystem = com.mobile.ads.ads.sdk. You can also filter logs by category and error level.
If you're having problems integrating ads, you'll get a detailed report on the issues and recommendations for how to fix them.
Additional resources
-
Link to GitHub.