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
- To integrate interstitial ads in your iOS app, import
VungleAdsSDK
and add an instance variable forVungleInterstitial
in yourViewController.swift
file (orViewController.h
file for Objective-c).import UIKit import VungleAdsSDK class LOInterstitialViewController: UIViewController { private var interstitialAd: VungleInterstitial?
#import "LOInterstitialViewController.h" #import <VungleAdsSDK/VungleAdsSDK.h> @interface LOInterstitialViewController () <VungleInterstitialDelegate> @property (nonatomic, strong) VungleInterstitial *interstitialAd; @end
- Add the following code to your method where you want to prepare the
VungleInterstitial
instance. To create theVungleInterstitial
, you will need a Placement ID.self.interstitialAd = VungleInterstitial(placementId: <YOUR_PLACEMENT_ID>) self.interstitialAd?.delegate = self
self.interstitialAd = [[VungleInterstitial alloc] initWithPlacementId:<YOUR_PLACEMENT_ID>]; self.interstitialAd.delegate = self;
Note: You must set your
ViewController
(or any class that declares conformance toVungleInterstitialDelegate)
to the delegate property of theVungleInterstitial
instance (refer to the Register for Callbacks section below).
Load an Interstitial Ad
- Once you create the
VungleInterstitial
instance, you can load an ad by callingload()
.
self.interstitialAd = VungleInterstitial(placementId: <YOUR_PLACEMENT_ID>) self.interstitialAd?.delegate = self self.interstitialAd?.load()
self.interstitialAd = [[VungleInterstitial alloc] initWithPlacementId:<YOUR_PLACEMENT_ID>]; self.interstitialAd.delegate = self; [self.interstitialAd load:nil];
- Your
ViewController
(or any class that conforms toVungleInterstitialDelegate
) will receive notification of load success or error through delegate callback methods (refer to the Register for Callbacks section below).
Play an Interstitial Ad
- Once the interstitial ad is successfully loaded, you can display it by calling
present(with: <YOUR_ViewController>)
.self.interstitialAd?.present(with: <YOUR_ViewController>)
[self.interstitialAd presentWith:<YOUR_ViewController>];
- 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).
- 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.
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") } }
#pragma mark - VungleInterstitial Delegate Methods // Ad load events - (void)interstitialAdDidLoad:(VungleInterstitial *)interstitial { NSLog(@"interstitialAdDidLoad"); } - (void)interstitialAdDidFailToLoad:(VungleInterstitial *)interstitial withError:(NSError *)withError { NSLog(@"interstitialAdDidFailToLoad"); } // Ad Lifecycle Events - (void)interstitialAdWillPresent:(VungleInterstitial *)interstitial { NSLog(@"interstitialAdWillPresent"); } - (void)interstitialAdDidPresent:(VungleInterstitial *)interstitial { NSLog(@"interstitialAdDidPresent"); } - (void)interstitialAdDidFailToPresent:(VungleInterstitial *)interstitial withError:(NSError *)withError { NSLog(@"interstitialAdDidFailToPresent"); } - (void)interstitialAdDidTrackImpression:(VungleInterstitial *)interstitial { NSLog(@"interstitialAdDidTrackImpression"); } - (void)interstitialAdDidClick:(VungleInterstitial *)interstitial { NSLog(@"interstitialAdDidClick"); } - (void)interstitialAdWillLeaveApplication:(VungleInterstitial *)interstitial { NSLog(@"interstitialAdWillLeaveApplication"); } - (void)interstitialAdWillClose:(VungleInterstitial *)interstitial { NSLog(@"interstitialAdWillClose"); } - (void)interstitialAdDidClose:(VungleInterstitial *)interstitial { NSLog(@"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: