Links

Banner Ad

This article will go through all the steps required to display an Banner Ad in your application.
Banner Ads are small or medium rectangle ads that have specific dimensions and occupy a certain position of the screen width and they can be freely integrated within the content of the app.
The banner comes with two predefined dimensions:
  • small banner (320x50)
  • MPU (300x250)

Requirements

You have registered an application on your Ogury Dashboard. If not, please refer to the Getting Started section before the next steps.

Step 1: Create a Banner Ad ad unit

  • Go to your Asset list in the Ogury Dashboard and select your Asset.
  • Click on the Monetization settings line in the left-menu.
  • Copy the Ad unit id as you will need this information later to finish the integration.
In all the following code samples, we will refer to this Ad unit id by using the string AD_UNIT_ID.

Step 2: Load a Banner

The Ogury SDK provides the OguryBannerAd view that lets you load, display and control your Banner Ad.

Instantiate a Banner

  • Declare an OguryBannerAd instance variable in the ViewController where you want to display a Banner:
Swift
Objective-C
var bannerAd: OguryBannerAd?
@property (nonatomic, strong) OguryBannerAd *bannerAd;
OguryBannerAdtakes the following parameter:
  • the Ad unit id of the Banner Ad. If you do not have one yet, you can refer to the first step to create it.

Load a Banner

To start loading an ad, call the load method:
Swift
Objective-C
bannerAd?.load(with: OguryAdsBannerSize);
[self.bannerAd loadWithSize:OguryAdsBannerSize];
loadWithSizetakes the following parameter:
  • the size of the Banner. It can take one of the value defined in OguryAdsBannerSize class:
OguryAdsBannerSize method
Definition
small_banner_320x50
Small Banner Ad (320x50)
mpu_300x250
Mid Page Unit Banner Ad (300x250)
Since it may take a few seconds to fetch the ad resources (video, image, ...) from the network, you should call the load method as soon as possible after getting the user's consent.
Ideally, you should implement one of the two following examples depending on your use case:
  • Call the load method right after the ask method in the ViewController that collects the consent:
Swift
Objective-C
import UIKit
import OgurySdk
import OguryAds
import OguryChoiceManager
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let configuration = OguryConfigurationBuilder(assetKey: "OGY-XXXXXXXXXXXX").build()
Ogury.start(with: configuration)
let bannerAd = OguryBannerAd(adUnitID: "AD_UNIT_ID")
bannerAd.load(with: OguryAdsBannerSize.small_banner_320x50())
}
}
#import <UIKit/UIKit.h>
#import <OgurySdk/Ogury.h>
#import <OguryAds/OguryAds.h>
#import <OguryChoiceManager/OguryChoiceManager.h>
@interface ViewController : UIViewController
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
OguryConfigurationBuilder *configurationBuilder = [[OguryConfigurationBuilder alloc] initWithAssetKey:@"OGY-XXXXXXXXXXXX"];
[Ogury startWithConfiguration:[configurationBuilder build]];
OguryBannerAd *bannerAd = [[OguryBannerAd alloc] initWithAdUnitId:@"AD_UNIT_ID"];
[bannerAd loadWithSize:[OguryAdsBannerSize small_banner_320x50]];
}
@end
  • Call the load method in the viewDidLoad of your other ViewController:
Swift
Objective-C
override func viewDidLoad() {
super.viewDidLoad()
let bannerAd = OguryBannerAd(adUnitID: "AD_UNIT_ID")
bannerAd?.load(with: OguryAdsBannerSize.small_banner_320x50())
}
- (void)viewDidLoad {
[super viewDidLoad];
OguryBannerAd *bannerAd = [[OguryAdsBanner alloc] initWithAdUnitId:@"AD_UNIT_ID"];
[bannerAd loadWithSize:[OguryAdsBannerSize small_banner_320x50]];
}
Additionally, if you want to follow the lifecycle of the Banner, you can register a delegate.

Step 3: Show a Banner

To display the Banner Ad, you simply need to attach the OguryBannerAd to one of your view:
Swift
Objective-C
yourView.addSubview(bannerAd)
[yourView addSubview:bannerAd];
We recommend to attach the Banner Ad once it is loaded, see example.

Step 4: Test your integration

As our algorithm works with personified targeting, you may not receive any ad while testing your application.
You can test your integration by adding the suffix _test to your interstitial ad unit id, for more details go to Test your implementation page.
Note that if you have just registered your application in the Ogury Dashboard, it will take around 15 minutes until you can successfully load an ad.
If you are not able to display any Banner, we recommend you to log every delegate on your OguryBannerAd to follow the lifecycle of the Banner Ad.
If you encounter the didFailOguryBannerAdWithErrorcallback , you can check the error codes section below to understand the error and get some advice on how to solve the issue.

Examples

Show an ad to a user entering your application

You may want to show a Banner Ad to a user as soon as they enter your application.
You can achieve this behavior by using the didLoadOguryBannerAd delegate to display the Banner Ad as soon as it is displayable. In the viewDidLoad of the first ViewController you can append the following lines:
Swift
Objective-C
import UIKit
import OgurySdk
import OguryAds
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let bannerAd = OguryBannerAd(adUnitID: "AD_UNIT_ID")
bannerAd?.delegate = self
bannerAd?.frame = CGRect(origin: <#T##CGPoint#>, size: <#T##CGSize#>)
bannerAd?.load(with: OguryAdsBannerSize.small_banner_320x50())
}
}
extension ViewController: OguryBannerAdDelegate {
func didLoadOguryBannerAd(_ banner: OguryBannerAd) {
view.addSubview(banner)
}
}
#import <UIKit/UIKit.h>
#import <OgurySdk/Ogury.h>
#import <Oguryads/OguryAds.h>
@interface ViewController : UIViewController <OguryAdsBannerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
OguryBannerAd *banner = [[OguryBannerAd alloc] initWithAdUnitId:@"AD_UNIT_ID"];
banner.delegate = self;
banner.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
[banner loadWithSize:[OguryAdsBannerSize small_banner_320x50]];
}
- (void)didLoadOguryBannerAd:(OguryBannerAd *)banner {
[self.view addSubview:banner];
}
@end

Advanced Topics

Check if a Banner is loaded

Call the following method to check if a Banner Ad is ready to be displayed:
Swift
Objective-C
bannerAd?.isLoaded()
[self.bannerAd isLoaded];
If you want to be informed when the ad is ready to be displayed, you can also register a delegate and implement the oguryBannerAdLoaded method.

Using modal View Controller

In case of using a modal View Controller, you need to provide the presenting UIViewController in order to allow the click on the banner via the delegate presentingViewControllerForOguryAdsBannerAd.
Swift
Objective-C
extension ViewController: OguryBannerAdDelegate {
func presentingViewController(forOguryAdsBannerAd banner: OguryBannerAd) -> UIViewController {
return self
}
}
- (UIViewController *)presentingViewControllerForOguryAdsBannerAd:(OguryBannerAd*)banner {
return viewController;
}

Delegate

The Ogury SDK provides OguryBannerAdDelegate to listen to the lifecycle of a Banner Ad instance.
To register a OguryBannerAdDelegate, add the following code just after instantiating the OguryBannerAd :
Swift
Objective-C
bannerAd?.delegate = self
self.bannerAd.delegate = self;
The OguryBannerAdDelegate exposes the following methods:
Methods
Definition
didLoadOguryBannerAd
The SDK is ready to display the ad provided by the ad server.
didDisplayOguryBannerAd
The ad has been displayed on the screen.
didClickOguryBannerAd
The as has been clicked by the user.
didCloseOguryBannerAd
The ad has been closed by the user.
didFailOguryBannerAdWithError
The ad failed to load or display. The error parameter contains the reason of the failure. All error codes are detailed in the section below.
didTriggerImpressionOguryBannerAd
The ad has triggered an impression.
presentingViewControllerForOguryAdsBannerAd
Pass to the SDK the presenting UIViewController in order to show banner modal when a tap event is made. more information here.

Error codes

When an ad fails to load or to be displayed, the SDK will call the didFailOguryBannerAdWithError callback with one of error code defined in OguryAdsError:
Name
Value
Definition
OguryCoreErrorTypeNoInternetConnection
0
No Internet connection
The device has no Internet connection. Try again once the device is connected to the Internet.
OguryAdsAdDisabledError
2001
Ad disabled
Ad serving has been disabled for this placement/application.
OguryAdsProfigNotSyncedError
2002
Profig not synchronized
An internal SDK error has occurred.
OguryAdsAdExpiredError
2003
Ad expired
The loaded ad is expired. You must call the show method within 4 hours after the load.
OguryAdsSdkInitNotCalledError
2004
SDK init not called
The Ogury.start() method has not been called before a call to the load or show methods.
OguryAdsAnotherAdAlreadyDisplayedError
2005
Another ad already displayed
Another ad is already displayed on the screen.
OguryAdsAssetKeyNotValidError
2006
SDK init failed
An error occurred during the initialization of the SDK.
OguryAdsNotAvailableError
2008
Ad not available
The server returns no ads.
OguryAdsCantShowAdsInPresentingViewControllerError
2010
A view controller is already being presented
Only one view controller may be presented at a time.
OguryAdsUnknownError
2011
Unknown error
An unknown error occurred.