Banner Ads
Overview
A Banner ad typically appears as a rectangular or square graphic or text on your app's user interface. Banner ads are mostly displayed at the top or bottom of the screen. Inline banner ads are placed inside the scrollable contents.
Create a BannerView
Banner ads are displayed using a BannerView
instance. You can create a BannerView
instance either programmatically or from interface builder.
Programmatically
Following code shows, initializing a BannerView
instance with a AdSize
and adding it to the view using autolayout.
- Objective C
- Swift
#import <BlueStackSDK/BlueStackSDK.h>
@interface ViewController ()
@property(nonatomic, strong) BannerView *bannerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize the BannerView instance
self.bannerView = [[BannerView alloc] initWithAdSize:AdSizeBanner];
// Add BannerView to the parent view
[self addBannerViewToView:self.bannerView];
}
- (void)addBannerViewToView:(UIView *)bannerView {
bannerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bannerView];
[self.view addConstraints:@[
[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view.safeAreaLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]
]];
}
@end
import BlueStackSDK
import UIKit
class ViewController: UIViewController {
var bannerView: BannerView!
override func viewDidLoad() {
super.viewDidLoad()
// Initialize the BannerView instance
bannerView = BannerView(adSize: .banner)
// Add BannerView to the parent view
addBannerViewToView(bannerView)
}
func addBannerViewToView(_ bannerView: BannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: view.safeAreaLayoutGuide,
attribute: .bottom,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}
}
Interface Builder
In your storyboard or xib add a UIView
and assign BannerView
to the class of that view in Identity Inspector. Add required position layout constraints to the view. Add a IBOutlet
to the BannerView
.
Load a banner ad
After initializing and adding BannerView
to the view hierarchy you need to load the BannerView
by calling load(requestOptions:)
method with a RequestOptions
object or simply calling load()
method.
Without RequestOptions
- Objective C
- Swift
self.bannerView.placementID = @"BANNER_PLACEMENT_ID_HERE";
self.bannerView.viewController = self;
[self.bannerView load];
bannerView.placementID = "BANNER_PLACEMENT_ID_HERE"
bannerView.viewController = self
bannerView.load()
To load banner ad, it is mandetory to provide placementID to BannerView
instance. It also takes a optional viewController
from which it will present full screen content after user interacts with the BannerView
.
With RequestOptions
Create RequestOptions
for supplying targeting information while loading ads.
- Objective C
- Swift
RequestOptions *requestOptions = [[RequestOptions alloc] initWithAge:@(25)
location:[[CLLocation alloc] initWithLatitude:48.87610 longitude:10.453]
gender:GenderMale
keyword:@"brand=myBrand;category=sport"
contentUrl:@"https://my_content_url.com/"];
[self.bannerView loadWithRequestOptions:requestOptions];
let requestOptions = RequestOptions(
age: 25,
location: CLLocation.init(latitude: 48.87610, longitude: 10.453),
gender: .male,
keyword: "brand=myBrand;category=sport",
contentUrl: "https://my_content_url.com/"
)
bannerView.load(requestOptions: requestOptions)
Ad events
To get BannerView
lifecycle events, you need to set the BannerViewDelegate
before loading the banner ad.
Register for BannerView events
- Objective C
- Swift
self.bannerView.delegate = self; // typically it is the viewController in which you are adding the BannerView
bannerView.delegate = self // typically it is the viewController in which you are adding the BannerView
If you are using interface builder you can set the delegate from the connections inspector.
Implement banner events
BannerView has the following events for notifying it's lifecycle, receiving click and resizes.
onLoad(bannerView:preferredHeight:)
will be called by the SDK when banner ad finishes loading.
- Objective C
- Swift
- (void)bannerView:(BannerView * _Nonnull)bannerView didLoadWithPreferredHeight:(CGFloat)preferredHeight {
NSLog(@"BannerView successfully loaded");
}
func onLoad(_ bannerView: BlueStackSDK.BannerView, _ preferredHeight: CGFloat) {
print("Banner successfully loaded with preferredHeight: \(preferredHeight)")
}
onFailedToLoad(bannerView:error:)
will be called when all the ad server fails to load the ad. It will return the error of last called ad server.
- Objective C
- Swift
- (void)bannerView:(BannerView * _Nonnull)bannerView didFailedToLoadWithError:(NSError * _Nonnull)error {
NSLog(@"BannerView failed to load ad with error: %@", error.localizedDescription);
}
func onFailedToLoad(_ bannerView: BlueStackSDK.BannerView, _ error: any Error) {
print("Banner Ad failed to load with error: \(error.localizedDescription)")
}
onRefresh(bannerView:)
will be called when the banner has refreshed
- Objective C
- Swift
- (void)didRefreshBannerView:(BannerView * _Nonnull)bannerView {
NSLog(@"BannerView refreshed");
}
func onRefresh(_ bannerView: BlueStackSDK.BannerView) {
print("Banner Ad refreshed")
}
onFailedToRefresh(bannerView:error:)
will be called when the banner fail to refresh.
- Objective C
- Swift
- (void)bannerView:(BannerView * _Nonnull)bannerView didFailedToRefreshWithError:(NSError * _Nonnull)error {
NSLog(@"BannerView failed to refresh ad with error: %@", error.localizedDescription);
}
func onFailedToRefresh(_ bannerView: BlueStackSDK.BannerView, _ error: any Error) {
print("Banner Ad failed to refresh with error: \(error.localizedDescription)")
}
onClick(bannerView:)
will be called when user click the banner ad.
- Objective C
- Swift
- (void)didClick:(BannerView *)bannerView {
NSLog(@"BannerView received a click");
}
func onClick(_ bannerView: BlueStackSDK.BannerView) {
print("Banner Ad clicked")
}
onResize(bannerView:size:)
will be called when the banner has changed size
- Objective C
- Swift
- (void)bannerView:(BannerView *)bannerView didResizedToSize:(CGSize)size {
NSLog(@"BannerView size has been changed to: %@", NSStringFromCGSize(size));
}
func onResize(_ bannerView: BlueStackSDK.BannerView, _ size: CGSize) {
print("Banner Ad resized to size: \(size)")
}
Destroying Banner Ad
You need to keep a strong reference to the BannerView
instance, if you want to add the banner view to the parent view after it completes loading a banner ad. A BannerView
instance maintains lifecycle of a single banner ad even after it refresh.
You must not call load(requestOptions:)
method multiple times using single BannerView
instance.
Also when you are done with BannerView
instance you can release the instance as follows:
- Objective C
- Swift
[self.bannerView removeFromSuperview];
self.bannerView.delegate = nil;
self.bannerView = nil;
bannerView.removeFromSuperview()
bannerView.delegate = nil
bannerView = nil
Banner Ad sizes
BannerView provides AdSize
enum for it's available ad sizes.
AdSize | Description | Dimensions |
---|---|---|
banner | Small Banner | 320 x 50 |
dynamicBanner | Small Banner Screen | Screen width x 50 |
largeBanner | Large Banner | 320 x 100 |
fullBanner | Full Banner ipad | 468 x 60 |
dynamicLeaderboardBanner | Landscape Banner ipad | 728 x 90 |
leaderboard | Landscape Banner ipad | Screen width x 90 |
mediumRectangle | Square Banner | 300 x 250 |
Adapt the banner size after loading
Although the BannerView
instance takes care of resizing the creative to make it fit properly in the banner view size, you can also resize the banner view to match the creative’s width/height.
The current creative's height can be fetched directly from the preferredHeight
parameter retrieved in the onLoad(_ bannerView: BannerView, _ preferredHeight: CGFloat)
delegate method.
You can adjust the height of your banner view using that preferredHeight
- Objective C
- Swift
- (void)bannerView:(BannerView * _Nonnull)bannerView didLoadWithPreferredHeight:(CGFloat)preferredHeight {
dispatch_async(dispatch_get_main_queue(), ^{
self.bannerViewHeightConstraint.constant = preferredHeight;
[self.view layoutIfNeeded];
});
}
func onLoad(_ bannerView: BlueStackSDK.BannerView, _ preferredHeight: CGFloat) {
DispatchQueue.main.async {
self.banerViewHeightConstraint.constant = preferredHeight;
self.view.layoutIfNeeded()
}
}
You will found size of the refreshed banner ad from the size
parameter of onResize(_ bannerView: BannerView, _ size: CGSize)
delegate method.
- Objective C
- Swift
- (void)bannerView:(BannerView *)bannerView didResizedToSize:(CGSize)size {
dispatch_async(dispatch_get_main_queue(), ^{
self.bannerViewWidthConstraint.constant = size.width;
self.bannerViewHeightConstraint.constant = size.height;
[self.view layoutIfNeeded];
});
}
func onResize(_ bannerView: BlueStackSDK.BannerView, _ size: CGSize) {
DispatchQueue.main.async {
self.banerViewHeightConstraint.constant = size.height;
self.banerViewWidthConstraint.constant = size.width;
self.view.layoutIfNeeded()
}
}