Freigeben über


Show interstitial ads on iOS

This page has instructions and code samples for showing interstitial ads on iOS.

Overview

Showing interstitial ads requires a little bit of more effort. In addition to implementing viewDidLoad as we did in the banner example, we must implement the adDidReceiveAd delegate method. Following section explains about it in details with code samples.

Process

  1. Call loadAd to fetch the ad contents from our server and cache them locally. Note that any ad content is rendered in a WebView at the time it is fetched from the ad server and cached. This means that any third-party tracking pixels that are part of the ad content will be fired at the time of the call to loadAd, not when the call to displayAdFromViewController is made at a later time.
  2. When the ad content is actually received from the ad server, your implementation of the adDidReceiveAd callback is fired. In the example below, we call displayAdFromViewController right away, but your implementation could wait until it's more convenient for your app to show the interstitial ad. Note that the call to displayAdFromViewController needs to happen within approximately 4 minutes of the call to loadAd in order for the impression to be counted by Xandr.

Note

The close button appears after ten seconds by default. You can set the delay using ANInterstitialAd.setCloseDelay.

For more information, see the code sample below.

Creative media types supported in Interstitial Ad Unit

Media Type Description
Banner Recommended for most interstitial placements to maximize demand. Suitable for static or animated banners.
Interstitial Used for full-screen ads, including static images, MRAID, and HTML responsive formats.
VAST Video Supports video ads as of version 9.1.0. Ideal for serving video creatives when placement is enabled for video demand.

Code sample

The interstitial code samples below show how to request ads using the placement ID.

Note

Beginning with version RC2.8, you can initialize interstitials using a combination of member ID and inventory code instead of placement ID though it is still supported. Here is the method that shows the initialization using inventory code and member ID:

// iOS: ObjC code that uses inventory code and member ID instead of placement ID (optional)
-(instancetype)initWithMemberId:(NSInteger)memberId inventoryCode:(NSString *)inventoryCode;
 
// iOS: ObjC code to show an interstitial ad
#import "ViewController.h"
#import "ANInterstitialAd.h"
@interface ViewController () <ANInterstitialAdDelegate>
@property (nonatomic, strong)ANInterstitialAd *inter;
@end
@implementation ViewController : UIViewController
- (void)viewDidLoad {
    self.inter = [[ANInterstitialAd alloc] initWithPlacementId:@"1326299"];
    
    // set ourselves as the delegate so we can respond to the required adDidReceiveAd
    // message of the `ANAdDelegate protocol' (see implementation below)
    self.inter.delegate = self;
    
    // Load an ad!
    [self.inter loadAd];
}
- (void)adDidReceiveAd:(id<ANAdProtocol>)ad {
    [self.inter displayAdFromViewController:self];
}
- (void)adFailedToDisplay:(ANInterstitialAd *)ad {
    NSLog(@"Uh oh, the ad failed to display!");
}
@end