Links

Banner Ads

This guide shows how to display Banner Ads in your application.

What are Banner Ads?

Banner Ad Units display a rectangular creative within an app. They occupy a portion of the app layout and stay on the screen as the users interact with the app.

How to Display Banner Ads

Integrate & Initialize HyprMX

Before you start, make sure you have correctly integrated and initialized the HyprMX SDK. To integrate and initialize HyprMX, see our Setup Guide.

Initialize a BannerView

BannerViews are initialized with their placement name and ad size.

Placement Name

HyprMX utilizes placements to load and display an ad. Placements are used to represent a point in your application's workflow where you'd like to show an ad and placement names are a string that identify individual banner placements in your application.
If you don't have your placement information, please reach out to your account manager to configure your banner placements. In return, they will provide the names of the new placements.

Ad Size

Banners are initialized using one of the pre-defined ad sizes.
Constant
Size
kHyprMXAdSizeShort
300x50
kHyprMXAdSizeBanner
320x50
kHyprMXAdSizeMediumRectangle
300x250
kHyprMXAdSizeLeaderBoard
728x90
kHyprMXAdSizeSkyScraper
160x600
To initialize a new BannerView:
Swift
Objective-C
let bannerView:HyprMXBannerView = HyprMXBannerView.init(placementName: "Banner", adSize: kHyprMXAdSizeBanner)
HyprMXBannerView *bannerView = [[HyprMXBannerView alloc] initWithPlacementName:@"Banner" adSize:kHyprMXAdSizeBanner];
HyprMXBannerViews are not retained in memory by the HyprMX SDK. You can retain the view by adding it to the view stack by calling self.view.addSubview(), or you can store the Banner in a local variable in your controller until you're ready to add to the view.
Swift
Objective-C
class ViewController: UIViewController, HyprMXBannerDelegate, HyprMXInitializationDelegate
var bannerView:HyprMXBannerView?
override func viewDidLoad() {
super.viewDidLoad()
self.bannerView = HyprMXBannerView.init(placementName: "Banner", adSize: kHyprMXAdSizeBanner)
}
@interface ViewController ()
@property (strong, nonatomic) HyprMXBannerView *bannerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.bannerView = HyprMXBannerView *bannerView = [[HyprMXBannerView alloc] initWithPlacementName:@"Banner" adSize:kHyprMXAdSizeBanner];
}

Adding Banners to your View

Add your BannerView to your View and configure its frame to match your requested ad size. You can configure the frame directly or use Constraints like any other UIView.
Example Using Constraints:
Swift
Objective-C
override func viewDidLoad() {
super.viewDidLoad()
self.bannerView = HyprMXBannerView.init(placementName: "Banner", adSize: kHyprMXAdSizeBanner)
// Add constraints to control the view's size
self.bannerView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.bannerView)
self.bannerView.widthAnchor.constraint(equalToConstant: self.bannerView.adSize.width).isActive = true
self.bannerView.heightAnchor.constraint(equalToConstant: self.bannerView.adSize.height).isActive = true
}
- (void)viewDidLoad {
[super viewDidLoad];
self.bannerView = HyprMXBannerView *bannerView = [[HyprMXBannerView alloc] initWithPlacementName:@"Banner" adSize:kHyprMXAdSizeBanner];
// Add constraints to control the view's size
self.bannerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bannerView];
[self.bannerView.widthAnchor constraintEqualToConstant:self.bannerView.adSize.width].active = YES;
[self.bannerView.heightAnchor constraintEqualToConstant:self.bannerView.adSize.height].active = YES;
}
Example setting the BannerView's Frame:
Swift
Objective-C
override func viewDidLoad() {
super.viewDidLoad()
self.bannerView = HyprMXBannerView.init(placementName: "Banner", adSize: kHyprMXAdSizeBanner)
// Set the bannerView's frame and add to the UI
self.bannerView.frame = CGRect.init(x: 0, y: 0, width: self.bannerView.adSize.width, height: self.bannerView.adSize.height)
self.view.addSubview(self.bannerView)
}
- (void)viewDidLoad {
[super viewDidLoad];
self.bannerView = HyprMXBannerView *bannerView = [[HyprMXBannerView alloc] initWithPlacementName:@"Banner" adSize:kHyprMXAdSizeBanner];
// Set the bannerView's frame and add to the UI
self.bannerView.frame = CGRectMake(0, 0, self.bannerView.adSize.width, self.bannerView.adSize.height);
[self.view addSubview:self.bannerView];
}
For best visibility, you should horizontally center your HyprMXBannerView in your screen. For example, you can accomplish this using a constraint.
Swift
Objective-C
bannerView.centerXAnchor.constraint(equalTo:self.view.safeAreaLayoutGuide.centerXAnchor).isActive = true;
[bannerView.centerXAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.centerXAnchor].active = YES;

Loading Ads

To load a banner ad, run the following.
Swift
Objective-C
bannerView.loadAd()
[bannerView loadAd];
Your bannerView will load an ad and render it in its view. To receive ad load success and other lifecycle callbacks, implement the HyprMXBannerDelegate protocol and set a placementDelegate on your bannerView.

Setting a HyprMXBannerDelegate (Optional)

To receive lifecycle callbacks on your HyprMXBannerView, implement the HyprMXBannerDelegate protocol. All methods are optional and you can implement the methods you would like to receive callbacks for.
Swift
Objective-C
bannerView.placementDelegate = self
bannerView.placementDelegate = self
HyprMX holds a weak reference to the placement delegate, so you must be sure to retain the delegate.
See below for available methods.
Swift
Objective-C
./** Called in response to loadAd when an ad was loaded */
func adDidLoad(_ bannerView: HyprMXBannerView) {
}
/** Called in response to loadAd when there was an error loading an ad */
func adFailed(toLoad bannerView: HyprMXBannerView, error: Error) {
}
/** Called when the user clicks on the bannerView */
func adWasClicked(_ bannerView: HyprMXBannerView) {
}
/** Called when a banner click will open a full-screen modal */
func adDidOpen(_ bannerView: HyprMXBannerView) {
}
/** Called when a full-screen modal has been closed */
func adDidClose(_ bannerView: HyprMXBannerView) {
}
/** Called when a banner click will open another application */
func adWillLeaveApplication(_ bannerView: HyprMXBannerView) {
}
/** Called in response to loadAd when an ad was loaded */
-(void)adDidLoad:(HyprMXBannerView *)bannerView {
}
/** Called in response to loadAd when there was an error loading an ad */
-(void)adFailedToLoad:(HyprMXBannerView *)bannerView error:(NSError *)error {
}
/** Called when the user clicks on the bannerView */
-(void)adWasClicked:(HyprMXBannerView *)bannerView {
}
/** Called when a banner click will open a full-screen modal */
-(void)adDidOpen:(HyprMXBannerView *)bannerView {
}
/** Called when a full-screen modal has been closed */
-(void)adDidClose:(HyprMXBannerView *)bannerView {
}
/** Called when a banner click will open another application */
-(void)adWillLeaveApplication:(HyprMXBannerView *)bannerView {
}

Updating Ad Size / Reloading

You can reload another banner at any time by calling loadAd() on the bannerView. You can also update your adSize before loading a new ad by updating the bannerView.adSize property.
A common use case for this is updating to a new size and reloading on response to a device rotation.
To update Ad Size and Reload Ad on completion of size change:
Swift
Objective-C
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate { context in
} completion: { context in
// Update your banner's adSize to the new desired size
self.bannerView.adSize = kHyprMXAdSizeLeaderBoard
// Load a new banner ad
self.bannerView.loadAd()
}
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Update your banner's adSize to the new desired size
self.bannerView.adSize = kHyprMXAdSizeLeaderBoard;
// Load a new banner ad
[self.bannerView loadAd]
}];
}