iOS SDK - Interstitial ads
Interstitial ads are full-screen ads that cover your app's interface until the user closes them. Show them at natural break points, such as between levels or after the user finishes a task, so they don't interrupt the experience.
Before you start, install and initialize the SDK.
1. Get the interstitial ad object
Get the ad object for your ad unit ID and set its delegate. The SDK caches one instance per ad unit, so calling getInterstitialAd again with the same ID returns the same object.
private var interstitial: InterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
interstitial = A11.shared.getInterstitialAd(adUnitID: "adUnitId")
interstitial?.delegate = self
}
2. Implement the delegate
Set the delegate before calling load(on:), so you don't miss any events. When an ad closes or fails to show, load the next one. When loading fails, we recommend retrying with exponential backoff. See the full example.
extension MyViewController: InterstitialAdDelegate {
func onInterstitialAdLoaded(adInfo: AdInfo) {}
func onInterstitialAdLoadFailed(error: AdLoadError) {}
func onInterstitialAdShown(adInfo: AdInfo) {}
func onInterstitialAdShowFailed(adInfo: AdInfo?, error: AdShowError) {}
func onInterstitialAdClicked(adInfo: AdInfo) {}
func onInterstitialAdClosed(adInfo: AdInfo) {}
func onInterstitialAdRevenue(adInfo: AdInfo) {}
}
3. Load the ad
Load the ad well before you need it, so it's ready at the next break point.
4. Show the ad
Call show(on:) at a natural break point, once onInterstitialAdLoaded has fired.
Full example
class MyViewController: UIViewController {
private var interstitial: InterstitialAd?
private var isInterstitialReady = false
private var retryDelay: TimeInterval = 5
override func viewDidLoad() {
super.viewDidLoad()
interstitial = A11.shared.getInterstitialAd(adUnitID: "adUnitId")
interstitial?.delegate = self
loadInterstitial()
}
func loadInterstitial() {
interstitial?.load(on: self)
}
// Call at a natural break point, for example between levels
func showInterstitial() {
guard isInterstitialReady else { return }
isInterstitialReady = false
interstitial?.show(on: self)
}
}
extension MyViewController: InterstitialAdDelegate {
func onInterstitialAdLoaded(adInfo: AdInfo) {
isInterstitialReady = true
retryDelay = 5
}
func onInterstitialAdLoadFailed(error: AdLoadError) {
print("Interstitial load failed: \(error)")
// Retry with exponential backoff: 5 s, 10 s, 20 s and so on, up to 60 s
DispatchQueue.main.asyncAfter(deadline: .now() + retryDelay) { [weak self] in
self?.loadInterstitial()
}
retryDelay = min(retryDelay * 2, 60)
}
func onInterstitialAdShowFailed(adInfo: AdInfo?, error: AdShowError) {
print("Interstitial show failed: \(error)")
loadInterstitial()
}
func onInterstitialAdClosed(adInfo: AdInfo) {
// Resume your app and preload the next one
loadInterstitial()
}
func onInterstitialAdShown(adInfo: AdInfo) {}
func onInterstitialAdClicked(adInfo: AdInfo) {}
func onInterstitialAdRevenue(adInfo: AdInfo) {}
}