Add Interstitial Ads to an iOS App

The Audience Network allows you to monetize your iOS apps with Facebook ads. An interstitial ad is a full screen ad that you can show in your app. Follow this guide to display this type of ad unit. Or, if you're interested in other kinds of ad units, see a list of available types.

Let's implement the following interstitial ad placement.



Step 1: Load and Show Interstitial Ad View

Step 2: Verify Impression and Click Logging

Step 3: How to Debug When Ad Not Shown

Step 4: Test Ads Integration

Step 1: Load and Show Interstitial Ad View

Ensure you have completed the Audience Network Getting Started and iOS Getting Started guides before you proceed.

  1. After you have created a new project from iOS Getting Started guide, import FBAudienceNetwork, declare that ViewController implements the FBInterstitialAdDelegate protocol, and add an instance variable for the interstitial ad unit
    import UIKit
    import FBAudienceNetwork
    
    class ViewController: UIViewController, FBInterstitialAdDelegate {
      private var interstitialAd: FBInterstitialAd?
    }
    #import <UIKit/UIKit.h>
    @import FBAudienceNetwork;
    
    @interface ViewController : UIViewController <FBInterstitialAdDelegate>
    @property (nonatomic, strong) FBInterstitialAd *interstitialAd;
    @end

  2. Next, instantiate the ad object in the view controller's viewDidLoad method, and implement interstitialAdDidLoad
    override func viewDidLoad() {
      super.viewDidLoad()
    
      // Instantiate an InterstitialAd object.
      // NOTE: the placement ID will eventually identify this as your app. You can ignore it while you are testing
      // and replace it later when you have signed up.
      // While you are using this temporary code you will only get test ads and if you release
      // your code like this to the App Store your users will not receive ads (you will get a 'No Fill' error).
      let interstitialAd = FBInterstitialAd(placementID: "YOUR_PLACEMENT_ID")
      interstitialAd.delegate = self
    
      // For auto play video ads, it's recommended to load the ad at least 30 seconds before it is shown
      interstitialAd.load()
      
      self.interstitialAd = interstitialAd
    }
    
    func interstitialAdDidLoad(_ interstitialAd: FBInterstitialAd) {
      guard interstitialAd.isAdValid else {
        return
      }
      print("Ad is loaded and ready to be displayed")
      interstitialAd.show(fromRootViewController: self)
    }
    - (void)viewDidLoad 
    {
      [super viewDidLoad];
    
      // Instantiate an InterstitialAd object. 
      // NOTE: the placement ID will eventually identify this as your app. You can ignore it while you are testing 
      // and replace it later when you have signed up.
      // While you are using this temporary code you will only get test ads and if you release
      // your code like this to the App Store your users will not receive ads (you will get a 'No Fill' error).
      self.interstitialAd = [[FBInterstitialAd alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID"];
    
      self.interstitialAd.delegate = self;
    
      // For auto play video ads, it's recommended to load the ad at least 30 seconds before it is shown
      [self.interstitialAd loadAd];
    }
    
    - (void)interstitialAdDidLoad:(FBInterstitialAd *)interstitialAd
    {
      NSLog(@"Ad is loaded and ready to be displayed");
    
      if (interstitialAd && interstitialAd.isAdValid) {
        // You can now display the full screen ad using this code:
        [interstitialAd showAdFromRootViewController:self];
      }
    }

  3. The ID that displays at YOUR_PLACEMENT_ID is a temporary ID for test purposes only.

    If you use this temporary ID in your live code, your users will not receive ads (they will get a No Fill error). You must return here after testing and replace this temporary ID with a live Placement ID.

    To find out how the generate a live Placement ID, refer to Audience Network Setup

    Choose your build target to be device and run the above code, you should see something like this:

When running ads in the simulator, change the setting to test mode to view test ads. Please go to How to Use Test Mode for more information.

Do not call loadAd on the FBInterstitialAd while the ad is being shown on the screen. If you need to load another FBInterstitialAd for future use, you can do so after the user closed the current one, for example in the interstitialAdDidClose callback.

Step 2: Verify Impression and Click Logging

Optionally, you can add the following functions to handle the cases when the ad is shown, clicked or closed by users

func interstitialAdWillLogImpression(_ interstitialAd: FBInterstitialAd) {
  print("The user sees the ad")
  // Use this function as indication for a user's impression on the ad.
}

func interstitialAdDidClick(_ interstitialAd: FBInterstitialAd) {
  print("The user clicked on the ad and will be taken to its destination")
  // Use this function as indication for a user's click on the ad.
}

func interstitialAdWillClose(_ interstitialAd: FBInterstitialAd) {
  print("The user clicked on the close button, the ad is just about to close")
  // Consider to add code here to resume your app's flow
}

func interstitialAdDidClose(_ interstitialAd: FBInterstitialAd) {
  print("The user clicked on the close button, the ad is just about to close")
  // Consider to add code here to resume your app's flow
}
- (void)interstitialAdWillLogImpression:(FBInterstitialAd *)interstitialAd 
{
  NSLog(@"The user sees the ad");
  // Use this function as indication for a user's impression on the ad.
}

- (void)interstitialAdDidClick:(FBInterstitialAd *)interstitialAd
{
  NSLog(@"The user clicked on the ad and will be taken to its destination");
  // Use this function as indication for a user's click on the ad.
}

- (void)interstitialAdWillClose:(FBInterstitialAd *)interstitialAd
{
  NSLog(@"The user clicked on the close button, the ad is just about to close");
  // Consider to add code here to resume your app's flow
}

- (void)interstitialAdDidClose:(FBInterstitialAd *)interstitialAd
{
  NSLog(@"Interstitial had been closed");
  // Consider to add code here to resume your app's flow
}

Step 3: Debugging When Ad Is Not Shown

Add and implement the following function in your View Controller implementation file to handle ad loading failures

func interstitialAd(_ interstitialAd: FBInterstitialAd, didFailWithError error: Error) {
  print("Interstitial ad failed to load with error: \(error.localizedDescription)")
}
- (void)interstitialAd:(FBInterstitialAd *)interstitialAd didFailWithError:(NSError *)error
{
  NSLog(@"Interstitial ad failed to load with error: %@", error);
}

When there is no ad to show, the interstitialAd:didFailWithError: will be called with error.code set to 1001. If you use your own custom reporting or mediation layer you might want to check the code value and detect this case. You can fallback to another ad network in this case, but do not re-request an ad immediately after.

Next Steps