Migrate to interstitial ad unit API for iOS

Transition to the LevelPlay Interstitial APIs by using an ad unit ID to load and display interstitial ads.

Read time 8 minutes

This guide explains how to transition to the LevelPlay Interstitial APIs (using an ad unit ID) from your current implementation, to load and display interstitial ads.

Prerequisites

  • The min supported SDK is 8.6.0. You can download the latest SDK here.
  • Make sure to initialize the SDK using LevelPlay Initialization API.
  • Find the AdUnitID in LevelPlay dashboard.

Create Interstitial Ad Object

The creation of the interstitial ad object must be performed after receiving the onInitSuccess callback.

The object is a reusable instance that can handle multiple loads and shows throughout the session. After creation, it should be used to load and show ads for the same ad unit.

For more advanced implementations, you may create multiple interstitial ad objects if necessary.

[code-snippet-ios]

self.interstitialAd = [[LPMInterstitialAd alloc] initWithAdUnitId:@"adUnitId"];
self.interstitialAd = LPMInterstitialAd(adUnitId: "adUnitId")

[/code-snippet-ios]

Implement Delegates

Implement the LPMInterstitialAdDelegate in your code instead of LevelPlayInterstitialDelegate to get informed of ad delivery. 

  • It is recommended to set the delegate before loading the interstitial ad.
  • Each interstitial ad should have its own delegate implementation.
  • Delegate methods run on the main thread.

[code-snippet-ios]

self.interstitialAd = [[LPMInterstitialAd alloc] initWithAdUnitId:@"adUnitId"]; 
self.interstitialAd.delegate = self;
#pragma mark - LPMInterstitialAdDelegate Methods
- (void)didLoadAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didFailToLoadAdWithAdUnitId:(NSString *)adUnitId error:(NSError *)error {}
- (void)didChangeAdInfo:(LPMAdInfo *)adInfo {}
- (void)didDisplayAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didFailToDisplayAdWithAdInfo:(LPMAdInfo *)adInfo error:(NSError *)error {}
- (void)didClickAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didCloseAdWithAdInfo:(LPMAdInfo *)adInfo {}
self.interstitialAd = LPMInterstitialAd(adUnitId: "adUnitId") 
self.interstitialAd.setDelegate(self)
  
// MARK: LPMInterstitialAdDelegate methods
func didLoadAd(with adInfo: LPMAdInfo) {}
func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) {}
func didChangeAdInfo(_ adInfo: LPMAdInfo) {}
func didDisplayAd(with adInfo: LPMAdInfo) {}
func didFailToDisplayAd(with adInfo: LPMAdInfo, error: Error) {}
func didClickAd(with adInfo: LPMAdInfo) {}
func didCloseAd(with adInfo: LPMAdInfo) {}

[/code-snippet-ios]

LegacyAd Unit (new)
Delegates LevelPlayInterstitialDelegateLPMInterstitialAdDelegate
EventsdidLoadWithAdInfodidLoadAdWithAdInfo
didFailToLoadWithErrordidFailToLoadAdWithAdUnitId
didOpenWithAdInfodidDisplayAdWithAdInfo
didCloseWithAdInfodidCloseAdWithAdInfo
didFailToShowWithErrordidFailToDisplayAdWithAdInfo
didClickWithAdInfodidClickAdWithAdInfo
didShowWithAdInfo- (deprecated)
-didChangeAdInfo

LevelPlay Ad Info

The ISAdInfo class returned by the interstitial listener callbacks has been replaced by LPMAdInfo.

Learn more about its implementation and available fields here.

Load Interstitial Ad

To load a interstitial ad, use loadAd instead of loadInterstitial.

[code-snippet-ios]

[self.interstitialAd loadAd];
self.interstitialAd.loadAd()

[/code-snippet-ios]

Show Interstitial Ad

Show an interstitial ad after you receive didLoadWithAdInfo callback, using the LPMInterstitialAdDelegate, instead of LevelPlayInterstitialDelegate. Use showAdWithViewController instead of showInterstitialWithViewController.

  • It is required to share ViewController
  • If using placements, pass the placement name in the showAd API as shown in the Placements section below.
  • After the ad has been successfully displayed to the user, you can load another ad by repeating the loading step. 

[code-snippet-ios]

- (void)showInterstitialAd {
    // Check that ad is ready
    if ([self.interstitialAd isAdReady]) {
        // Show without placement
        [self.interstitialAd showAdWithViewController:self placementName:NULL];
    }
}
func showInterstitialAd() {
    // Check that ad is ready  
    if self.interstitialAd.isAdReady() {
        // Show without placement
        self.interstitialAd.showAd(viewController: self, placementName: nil)
    }
}

[/code-snippet-ios]

Check Ad is Ready

To avoid show failures, and to make sure the ad could be displayed correctly, we recommend using the following API before calling the showAd API.

isAdReady – returns true if ad was loaded successfully and ad unit is not capped, or false otherwise.

isPlacementCapped – returns true when a valid placement is capped. If the placement is not valid, or not capped, this API will return false.

[code-snippet-ios]

// Check that ad is ready and that the placement is not capped 
if ([self.interstitialAd isAdReady] && ![LPMInterstitialAd isPlacementCapped:placementName]) {
    [self.interstitialAd showAdWithViewController:self placementName:placementName];
}

// Check that ad is ready and that the placement is not capped 
if self.interstitialAd.isAdReady(),!LPMInterstitialAd.isPlacementCapped(placementName) {
    self.interstitialAd.showAd(viewController: self, placementName: placementName)
}

[/code-snippet-ios]

Placements

We support placements pacing and capping for interstitial on the LevelPlay dashboard. 

If placements are set up for interstitial ads, call the showAd method to serve the ad for a specific placement.

[code-snippet-ios]

// Check that ad is ready and that the placement is not capped
 if ([self.interstitialAd isAdReady] && ![LPMInterstitialAd isPlacementCapped:placementName]) {
    // Show ad with placement
    [self.interstitialAd showAdWithViewController:self placementName:@"PlacementName"];
}
// Check that ad is ready and that the placement is not capped 
if self.interstitialAd.isAdReady(), !LPMInterstitialAd.isPlacementCapped(placementName) {
    // Show ad with placement
    self.interstitialAd.showAd(viewController: self, placementName: placementName) 
}

[/code-snippet-ios]

Multiple Ad Unit Interstitial APIs

LegacyAd Unit (new)
APIloadInterstitialloadAd
showInterstitialWithViewControllershowAdWithViewController
hasInterstitialisAdReady
isInterstitialCappedForPlacementisPlacementCapped

Full Implementation Example of Ad Unit API

[code-snippet-ios]

NS_ASSUME_NONNULL_BEGIN
@interface InterstitialAdViewController () <LPMInterstitialAdDelegate>
@property(nonatomic, strong) LPMInterstitialAd *interstitialAd;
@end
@implementation InterstitialAdViewController
- (void)createInterstitialAd {
    self.interstitialAd = [[LPMInterstitialAd alloc] initWithAdUnitId:@"adUnitId"];
    self.interstitialAd.delegate = self;
}
- (void)loadInterstitialAd {
    // Used to load or reload the ad
    [self.interstitialAd loadAd];
}
- (void)showInterstitialAd {
    if ([self.interstitialAd isAdReady]) {
        [self.interstitialAd showAdWithViewController:self placementName:NULL];
    }
}
- (void)showInterstitialAdWithPlacementName:(NSString *)placementName {
    // Check that ad is ready and that the placement is not capped 
    if ([self.interstitialAd isAdReady] && ![LPMInterstitialAd isPlacementCapped:placementName]) {
        [self.interstitialAd showAdWithViewController:self placementName:placementName];
    }
}
#pragma mark - LPMInterstitialAdDelegate Methods
- (void)didLoadAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didFailToLoadAdWithAdUnitId:(NSString *)adUnitId error:(NSError *)error {}
- (void)didChangeAdInfo:(LPMAdInfo *)adInfo {}
- (void)didDisplayAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didFailToDisplayAdWithAdInfo:(LPMAdInfo *)adInfo error:(NSError *)error {}
- (void)didClickAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didCloseAdWithAdInfo:(LPMAdInfo *)adInfo {}
@end
NS_ASSUME_NONNULL_END
class InterstitialAdViewController: UIViewController, LPMInterstitialAdDelegate {
    var interstitialAd: LPMInterstitialAd!
    
    func createInterstitialAd() {
        self.interstitialAd = LPMInterstitialAd(adUnitId: "adUnitId")
        self.interstitialAd.setDelegate(self)
    }
    
    func loadInterstitialAd() {
        // Used to load or reload the ad
        self.interstitialAd.loadAd()
    }
    
    func showInterstitialAd() {
        if self.interstitialAd.isAdReady() {
            self.interstitialAd.showAd(viewController: self, placementName: nil)
        }
    }
    
    func showInterstitialAd(withPlacementName placementName: String) {
        // Check that ad is ready and that the placement is not capped 
        if self.interstitialAd.isAdReady(), !LPMInterstitialAd.isPlacementCapped(placementName) {
            self.interstitialAd.showAd(viewController: self, placementName: placementName)
        }
    }
    
    // MARK: LPMInterstitialAdDelegate methods
    func didLoadAd(with adInfo: LPMAdInfo) {}
    func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) {}
    func didChangeAdInfo(_ adInfo: LPMAdInfo) {}
    func didDisplayAd(with adInfo: LPMAdInfo) {}
    func didFailToDisplayAd(with adInfo: LPMAdInfo, error: Error) {}
    func didClickAd(with adInfo: LPMAdInfo) {}
    func didCloseAd(with adInfo: LPMAdInfo) {}
}

[/code-snippet-ios]

LevelPlay Mediation Demo App

The Integration Demo application demonstrates how to integrate interstitial Ad Unit APIs in your app.

Download iOS Demo Application