Banner ads

This section provides details and examples of how to configure, load, and display a banner ad. You can display the ad when it loads, or on some other specific event; for example, when the user clicks a button.

Set up and load the banner ad

Before you can display an ad on your app or site, you first need to set the required configuration items, and then pass the ad placement ID to the load method. Make sure to set the callback block before you call the load method. For example:

import SuperAwesome

class ViewController: UIViewController {

  var bannerAd: BannerView!

  override func viewDidLoad() {
    super.viewDidLoad()
    let placementId: Int = 30471

    // create banner
    bannerAd = BannerView()

    // to display test ads
    bannerAd.enableTestMode()

    // set background color to transparent
    bannerAd.setColorTransparent()

    // set the banner event callback to play the ad immediately after the ad is loaded
    bannerAd.setCallback { [weak self] placementId, event in
      if event == .adLoaded {
        self?.bannerAd.play()
      }
    }

    // load the ad
    bannerAd.load(placementId)
  }
}
@import SuperAwesome;

@interface ViewController ()
@property (strong, nonatomic) SABannerAd *bannerAd;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  self.bannerAd = [[SABannerAd alloc] init];

  // to display test ads
  [_bannerAd enableTestMode];

  // set background color transparent
  [_bannerAd setColorTransparent];

  // set the banner event callback to play the ad immediately after the ad is loaded
  __weak ViewController *weakSelf = self;
  [_bannerAd setCallback:^(NSInteger placementId, SAEvent event) {
    if (event == SAEventAdLoaded) {
      [weakSelf.bannerAd play];
    }
  }];

  // start loading ad data for a placement
  [_bannerAd load:30471];
}

@end
Parameter Default value
Test mode Disabled
Background Color Gray

Aditional load parameters

You can also send additional options during the load call as well as an optional OpenRTB partner ID. Both parameters are optional.

let options = ["key": "value"]
let openRtbPartnerId = "1234"
bannerAd.load(30471, openRtbPartnerId: openRtbPartnerId, options: options)
NSDictionary *options = @{@"key": @"value"};
NSString *openRtbPartnerId = @"1234";
[_bannerAd load:30471 openRtbPartnerId:openRtbPartnerId options:options];

Display the banner ad

To display the ad, call play, using either the adLoaded callback or the hasAdAvailable method.

  • Use the adLoaded callback to display the ad as soon as it is loaded. Add the callback before you call the load method. For example:
...
bannerAd.setCallback { [weak self] placementId, event in
  if event == .adLoaded {
    self?.bannerAd.play()
  }
}
bannerAd.load(30471)
...
...
// add a callback (before calling load)
__weak ViewController *weakSelf = self;
[_bannerAd setCallback:^(NSInteger placementId, SAEvent event) {
    // when the ad loads, play it directly
    if (event == SAEventAdLoaded) {
        [weakSelf.bannerAd play];
    }
}];

// start the loading process
[_bannerAd load:30471];
...
  • Use the hasAdAvailable method to display the ad on some other event, for example when the user clicks a button in the app:
import SuperAwesome

class ViewController: UIViewController {

  var bannerAd = BannerView()

  override func viewDidLoad() {
    super.viewDidLoad()

    bannerAd.load(30471)
  }

  func playAdIfReady() {
    if bannerAd.hasAdAvailable() {
      bannerAd.play()
    }
  }
}
@import SuperAwesome;

@interface MyViewController()
@property (weak, nonatomic) IBOutlet SABannerAd *bannerAd;
@end

@implementation MyViewController

- (void) viewDidLoad {
    [super viewDidLoad];

    // load the banner ad
    [_bannerAd load:30471];

}

- (IBAction) onClick:(id) sender {

    // check if ad is loaded
    if ([_bannerAd hasAdAvailable]) {

         // display the ad
         [_bannerAd play];
    }
 }

@end