Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Showing Interstitial Ads

Sebastian Markowski edited this page Mar 10, 2022 · 31 revisions

Once you've performed the required project setup, you can display an AdColony interstitial ad in your app in a few easy steps:

Step 1: Configure AdColony

Configure the AdColony SDK using the app and zone IDs you created in the control panel.

/* Objective-C */

#import <AdColony/AdColony.h>



@implementation AppDelegate
/* Class body ... */

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [AdColony configureWithAppID:/* App ID */ 
                         options:nil
                      completion:^(NSArray<AdColonyZone *> *zones) {
                          // configured
                      }
    ];
    return YES;
}
/* Swift */

import AdColony



class AppDelegate: UIResponder, UIApplicationDelegate {
/* Class body ... */

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        AdColony.configure(withAppID: /* App ID */, options: nil) { [weak self] (zones) in
            // configured
        }
        return true
    }
}

Step 2: Request an Interstitial Ad

See Outdated APIs for changes.

Next, request an ad for a zone and store interstitial object for later use.

/* Objective-C */

#import <AdColony/AdColony.h>
#import "ViewController.h"



@interface ViewController() <AdColonyInterstitialDelegate>

@property (nonatomic, weak) AdColonyInterstiatial *interstitial;

@end



@implementation ViewController
/* Class body ... */


// request interstitial
- (void)requestInterstitial {
    [AdColony requestInterstitialInZone:/*Zone ID*/ options:nil andDelegate:self];
}

#pragma mark - AdColony Interstitial Delegate

// Store a reference to the returned interstitial object
- (void)adColonyInterstitialDidLoad:(AdColonyInterstitial *)interstitial {
    self.interstitial = interstitial;
}

// Handle loading error
- (void)adColonyInterstitialDidFailToLoad:(AdColonyAdRequestError *)error {
    NSLog(@"Interstitial did fail to load with error: %@ and suggestion: %@", error.localizedDescription, error.localizedRecoverySuggestion);
}

// Handle expiring ads (optional)
- (void)adColonyInterstitialExpired:(AdColonyInterstitial *)interstitial  {
    // remove stored ad, it's expired
    self.interstitial = nil;
    
    // you can request new ad
    [self requestInterstitial];
}

@end
/* Swift */

import AdColony



class ViewController: UIViewController, AdColonyInterstitialDelegate {

    weak var interstitial:AdColonyInterstitial?

    /* Class body ... */

    func requestInterstitial() {
        AdColony.requestInterstitial(inZone: Ads.shared.currentZone, options: nil, andDelegate: self)
    }

    // MARK:- AdColony Interstitial Delegate

    // Store a reference to the returned interstitial object
    func adColonyInterstitialDidLoad(_ interstitial: AdColonyInterstitial) {
        self.interstitial = interstitial
    }
    
    // Handle loading error
    func adColonyInterstitialDidFail(toLoad error: AdColonyAdRequestError) {
        print("Interstitial request failed with error: \(error.localizedDescription) and suggestion: \(error.localizedRecoverySuggestion!)")
    }
    
    // Handle expiring ads (optional)
    func adColonyInterstitialExpired(_ interstitial: AdColonyInterstitial) {
        // remove reference to stored ad
        self.interstitial = nil

        // you can request new ad
        self.requestInterstitial()
    }
    
}

Step 3: Show the Interstitial Ad

Using the AdColonyInterstitial object passed back via the success handler, show your ad object when appropriate.

/* Objective-C */

if (self.interstitial && !self.interstitial.expired) {
    [self.interstitial showWithPresentingViewController:self];
}
/* Swift */

if let interstitial = self.interstitial, !interstitial.expired {
    interstitial.show(withPresenting: self)
}

Step 4: Optional lifecycle events

You may like to receive some more events about the ad lifecycle, like when the ad opens or close, receives a click or sends user outside of the application. In order to get those signals you can implement delegate methods below:

/* Objective-C */

#import <AdColony/AdColony.h>
#import "ViewController.h"



@interface ViewController() <AdColonyInterstitialDelegate>
// ...
@end



@implementation ViewController
/* Class body ... */

- (void)adColonyInterstitialWillOpen:(AdColonyInterstitial *)interstitial {
    NSLog(@"Interstitial will open");
}

- (void)adColonyInterstitialDidClose:(AdColonyInterstitial *)interstitial {
    NSLog(@"Interstitial did close");
}

- (void)adColonyInterstitialWillLeaveApplication:(AdColonyInterstitial *)interstitial {
    NSLog(@"Interstitial will send user out of application");
}

- (void)adColonyInterstitialDidReceiveClick:(AdColonyInterstitial *)interstitial {
    NSLog(@"Interstitial did receive a click");
}

@end
/* Swift */

import AdColony



class ViewController: UIViewController, AdColonyInterstitialDelegate {
/* Class body ... */

    func adColonyInterstitialWillOpen(_ interstitial: AdColonyInterstitial) {
        print("Interstitial will open")
    }
    
    func adColonyInterstitialDidClose(_ interstitial: AdColonyInterstitial) {
        print("Interstitial did close")
    }
    
    func adColonyInterstitialWillLeaveApplication(_ interstitial: AdColonyInterstitial) {
        print("Interstitial will send user out of application")
    }
    
    func adColonyInterstitialDidReceiveClick(_ interstitial: AdColonyInterstitial) {
        print("Interstitial did receive a click")
    }
}