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

Showing Banner Ads

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

Once you've performed the required project setup, you can display an AdColony banner 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 */

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 banner ad

Choose the size for banner ad that best fits space for an ad in your app. You can choose from one of predefined sizes:

  • kAdColonyAdSizeBanner - Standard banner, 320x50
  • kAdColonyAdSizeMediumRectangle - Medium rectangle, 300x250
  • kAdColonyAdSizeLeaderboard - Leaderboard, 728x90
  • kAdColonyAdSizeSkyscraper - Skyscraper, 160x600

or define one yourself:

/* Objective-C */

AdColonyAdSize adSize = AdColonyAdSizeMake(320, 75);

// or

AdColonyAdSize adSize = AdColonyAdSizeFromCGSize(myPlacementView.frame.size);
/* Swift */

let adSize = AdColonyAdSizeMake(320, 75)

// or

let adSize = AdColonyAdSizeFromCGSize(myPlacementView.frame.size)

Then request a banner and add it to the view:

/* Objective-C */

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



@interface ViewController() <AdColonyAdViewDelegate>

@property (nonatomic, weak) AdColonyAdView *banner;
@property (nonatomic, weak) IBOutlet UIView *bannerPlacement;

@end



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

// request banner ad
- (void)requestBanner {
    [AdColony requestAdViewInZone:/* Zone ID */ withSize:kAdColonyAdSizeBanner viewController:self andDelegate:self];
}

#pragma mark - AdColony AdView Delegate

// handle new banner
- (void)adColonyAdViewDidLoad:(AdColonyAdView *)adView {
    if (self.banner) {
        // remove previous banner if exists
        [self.banner destroy];
    }
    
    // you can set AdView size to be the same as placement size
    // AdView will take care about banner centering
    CGRect placementSize = self.bannerPlacement.frame.size
    adView.frame = CGRectMake(0, 0, placementSize.width, placementSize.height);

    // add banner to view
    [self.bannerPlacement addSubview:adView];

    // store banner reference to be able to clear it later
    self.banner = adView;
}

// handler banner loading failure
- (void)adColonyAdViewDidFailToLoad:(AdColonyAdRequestError *)error {
    NSLog(@"Banner request failed with error: %@ and suggestion: %@", error.localizedDescription, error.localizedRecoverySuggestion);
}

@end
/* Swift */

import AdColony



class ViewController: UIViewController, AdColonyAdViewDelegate {

    weak var banner:AdColonyAdView?
    @IBOutlet weak var bannerPlacement:UIView!

    /* Class body ... */

    func requestBanner() {
        AdColony.requestAdView(inZone: /* Zone ID */, with: kAdColonyAdSizeBanner, viewController: self, andDelegate: self)
    }

    // MARK:- AdColony AdView Delegate

    // handle new banner
    func adColonyAdViewDidLoad(_ adView: AdColonyAdView) {
        if let oldBanner = self.banner {
            // remove previous banner if exists
            oldBanner.destroy()
        }
        
        // you can set AdView size to be the same as placement size
        // AdView will take care about banner centering
        let placementSize = self.bannerPlacement.frame.size
        adView.frame = CGRect(x: 0, y: 0, width: placementSize.width, height: placementSize.height)

        // add banner to view
        self.bannerPlacement.addSubview(adView)

        // store banner reference to be able to clear it later
        self.banner = adView
    }
    
    // handler banner loading failure
    func adColonyAdViewDidFail(toLoad error: AdColonyAdRequestError) {
        print("Banner request failed with error: \(error.localizedDescription) and suggestion: \(error.localizedRecoverySuggestion!)")
    }
}

Without UIViewController at the time of request:

/* Objective-C */

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



@interface ViewController() <AdColonyAdViewAdvancedDelegate>

@property (nonatomic, weak) AdColonyAdView *banner;
@property (nonatomic, weak) IBOutlet UIView *bannerPlacement;

@end



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

// request banner ad
- (void)requestBanner {
    [AdColony requestAdViewInZone:/* Zone ID */ withSize:kAdColonyAdSizeBanner options:nil andDelegate:self];
}

#pragma mark - AdColony AdView Delegate

// provide host view controller
- (UIViewController *)adColonyAdViewHostViewController:(AdColonyAdView *)adView {
    return self;
}

// handle new banner
- (void)adColonyAdViewDidLoad:(AdColonyAdView *)adView {
    if (self.banner) {
        // remove previous banner if exists
        [self.banner destroy];
    }
    
    // you can set AdView size to be the same as placement size
    // AdView will take care about banner centering
    CGRect placementSize = self.bannerPlacement.frame.size
    adView.frame = CGRectMake(0, 0, placementSize.width, placementSize.height);

    // add banner to view
    [self.bannerPlacement addSubview:adView];

    // store banner reference to be able to clear it later
    self.banner = adView;
}

// handler banner loading failure
- (void)adColonyAdViewDidFailToLoad:(AdColonyAdRequestError *)error {
    NSLog(@"Banner request failed with error: %@ and suggestion: %@", error.localizedDescription, error.localizedRecoverySuggestion);
}

@end
/* Swift */

import AdColony



class ViewController: UIViewController, AdColonyAdViewAdvancedDelegate {

    weak var banner:AdColonyAdView?
    @IBOutlet weak var bannerPlacement:UIView!

    /* Class body ... */

    func requestBanner() {
        AdColony.requestAdView(inZone: /* Zone ID */, with: kAdColonyAdSizeBanner, options: nil, andDelegate: self)
    }

    // MARK:- AdColony AdView Delegate
    
    // provide host view controller
    func adColonyAdViewHostViewController(_ adView: AdColonyAdView) -> UIViewController {
    	return self
    }

    // handle new banner
    func adColonyAdViewDidLoad(_ adView: AdColonyAdView) {
        if let oldBanner = self.banner {
            // remove previous banner if exists
            oldBanner.destroy()
        }
        
        // you can set AdView size to be the same as placement size
        // AdView will take care about banner centering
        let placementSize = self.bannerPlacement.frame.size
        adView.frame = CGRect(x: 0, y: 0, width: placementSize.width, height: placementSize.height)

        // add banner to view
        self.bannerPlacement.addSubview(adView)

        // store banner reference to be able to clear it later
        self.banner = adView
    }
    
    // handler banner loading failure
    func adColonyAdViewDidFail(toLoad error: AdColonyAdRequestError) {
        print("Banner request failed with error: \(error.localizedDescription) and suggestion: \(error.localizedRecoverySuggestion!)")
    }
}

Step 3: Destroy the Ad

It's pretty important to destroy banner view when it's not needed anymore to free the memory it's using. Destroy method will also take care about removing banner from view.

/* Objective-C */

[self.banner destroy];
/* Swift */

if let ad = self.banner {
    ad.destroy()
}

Step 4: Optional lifecycle events

You may like to receive some more events about the ad lifecycle, like when the ad shows fullscreen view, 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() <AdColonyAdViewDelegate>
// ...
@end



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

- (void)adColonyAdViewWillOpen:(AdColonyAdView *)adView {
    NSLog(@"AdView will open fullscreen view");
}

- (void)adColonyAdViewDidClose:(AdColonyAdView *)adView {
    NSLog(@"AdView did close fullscreen views");
}

- (void)adColonyAdViewWillLeaveApplication:(AdColonyAdView *)adView {
    NSLog(@"AdView will send used outside the app");
}

- (void)adColonyAdViewDidReceiveClick:(AdColonyAdView *)adView {
    NSLog(@"AdView received a click");
}

@end
/* Swift */

import AdColony



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

    func adColonyAdViewWillOpen(_ adView: AdColonyAdView) {
        print("AdView will open fullscreen view")
    }
    
    func adColonyAdViewDidClose(_ adView: AdColonyAdView) {
        print("AdView did close fullscreen views")
    }
    
    func adColonyAdViewWillLeaveApplication(_ adView: AdColonyAdView) {
        print("AdView will send used outside the app")
    }
    
    func adColonyAdViewDidReceiveClick(_ adView: AdColonyAdView) {
        print("AdView received a click")
    }
}