Integrate Interstitial Ads

To integrate any ad format, you must first have completed the instructions in the basic integration article.

Interstitial ads are full screen ads that cover the interface of your app. They're typically displayed at natural transition points in the flow of your app, such as between activities or during the pause between levels in a game.

Create an Interstitial Ad

  1. To integrate interstitial ads in your iOS app, import VungleAdsSDK and add an instance variable for VungleInterstitial in your ViewController.swift file (or ViewController.h file for Objective-c).
    SwiftObjective-C
    import UIKit
    import VungleAdsSDK
    
    class LOInterstitialViewController: UIViewController {
        private var interstitialAd: VungleInterstitial?
  2. Add the following code to your method where you want to prepare the VungleInterstitial instance. To create the VungleInterstitial, you will need a Placement ID.
    SwiftObjective-C
    self.interstitialAd = VungleInterstitial(placementId: <YOUR_PLACEMENT_ID>)
    self.interstitialAd?.delegate = self

    Note: You must set your ViewController (or any class that declares conformance to VungleInterstitialDelegate) to the delegate property of the VungleInterstitial instance (refer to the Register for Callbacks section below).

Load an Interstitial Ad

  1. Once you create the VungleInterstitial instance, you can load an ad by calling load().
    SwiftObjective-C
    self.interstitialAd = VungleInterstitial(placementId: <YOUR_PLACEMENT_ID>)
    self.interstitialAd?.delegate = self
    self.interstitialAd?.load()
  2. Your ViewController (or any class that conforms to VungleInterstitialDelegate) will receive notification of load success or error through delegate callback methods (refer to the Register for Callbacks section below).

Play an Interstitial Ad

  1. Once the interstitial ad is successfully loaded, you can display it by calling present(with: <YOUR_ViewController>).
    SwiftObjective-C
    self.interstitialAd?.present(with: <YOUR_ViewController>)
  2. During the time the interstitial ad is visible to the user, you can listen for ad lifecycle events through delegate callback methods (refer to the Register for Callbacks section below).
  3. The ad is closed when the user closes it. You do not need to add steps in your code to close it.

Register for Callbacks

To receive notifications for ad events, declare conformance to VungleInterstitialDelegate protocol and implement callback methods. Each of the callback methods is optional, so you only need to implement the methods you want.

SwiftObjective-C
extension LOInterstitialViewController: VungleInterstitialDelegate {
    // Ad load events
    func interstitialAdDidLoad(_ interstitial: VungleInterstitial) {
        print("interstitialAdDidLoad")
    }
    
    func interstitialAdDidFailToLoad(_ interstitial: VungleInterstitial, withError: NSError) {
        print("interstitialAdDidFailToLoad")
    }

    // Ad Lifecycle Events
    func interstitialAdWillPresent(_ interstitial: VungleInterstitial) {
        print("interstitialAdWillPresent")
    }
    
    func interstitialAdDidPresent(_ interstitial: VungleInterstitial) {
        print("interstitialAdDidPresent")
    }
    
    func interstitialAdDidFailToPresent(_ interstitial: VungleInterstitial, withError: NSError) {
        print("interstitialAdDidFailToPresent")
    }
    
    func interstitialAdDidTrackImpression(_ interstitial: VungleInterstitial) {
        print("interstitialAdDidTrackImpression")
    }
    
    func interstitialAdDidClick(_ interstitial: VungleInterstitial) {
        print("interstitialAdDidClick")
    }
    
    func interstitialAdWillLeaveApplication(_ interstitial: VungleInterstitial) {
        print("interstitialAdWillLeaveApplication")
    }
    
    func interstitialAdWillClose(_ interstitial: VungleInterstitial) {
        print("interstitialAdWillClose")
    }
    
    func interstitialAdDidClose(_ interstitial: VungleInterstitial) {
        print("interstitialAdDidClose")
    }
}

Test an Interstitial Ad

You can test an Interstitial ad in one of two ways: by setting your app status to Test Mode so that Vungle can deliver test ads to your app, or by adding a test device to your app so that Vungle can deliver test ads specifically to the test device. Follow the instructions in Test Your Integration: Test Mode and Test Devices.

Additional Resources

Refer to our Interstitial ad examples:

Questions?

Need further assistance, feel free to reach out to us, we’re here to help!

Was this article helpful?