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

Initialize HyprMX

To integrate and initialize HyprMX, see our Setup Guide.

Initialize a BannerView

BannerViews are initialized with their placement name and ad size.

Placement Name

Placement names are a string that identify individual banner placements in your application. 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
Swift
Objective-C
1
let bannerView:HyprMXBannerView = HyprMXBannerView.init(placementName: "Banner", adSize: kHyprMXAdSizeBanner)
Copied!
1
HyprMXBannerView *bannerView = [[HyprMXBannerView alloc] initWithPlacementName:@"Banner" adSize:kHyprMXAdSizeBanner];
Copied!
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
1
class ViewController: UIViewController, HyprMXBannerDelegate, HyprMXInitializationDelegate
2
var bannerView:HyprMXBannerView?
3
4
override func viewDidLoad() {
5
super.viewDidLoad()
6
self.bannerView = HyprMXBannerView.init(placementName: "Banner", adSize: kHyprMXAdSizeBanner)
7
}
Copied!
1
@interface ViewController ()
2
@property (strong, nonatomic) HyprMXBannerView *bannerView;
3
@end
4
5
@implementation ViewController
6
- (void)viewDidLoad {
7
[super viewDidLoad];
8
self.bannerView = HyprMXBannerView *bannerView = [[HyprMXBannerView alloc] initWithPlacementName:@"Banner" adSize:kHyprMXAdSizeBanner];
9
}
Copied!

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 using Constraints like any other UIView.
Example Using Constraints:
Swift
Objective-C
1
override func viewDidLoad() {
2
super.viewDidLoad()
3
self.bannerView = HyprMXBannerView.init(placementName: "Banner", adSize: kHyprMXAdSizeBanner)
4
5
// Add constraints to control the view's size
6
self.bannerView.translatesAutoresizingMaskIntoConstraints = false
7
self.view.addSubview(self.bannerView)
8
self.bannerView.widthAnchor.constraint(equalToConstant: self.bannerView.adSize.width).isActive = true
9
self.bannerView.heightAnchor.constraint(equalToConstant: self.bannerView.adSize.height).isActive = true
10
}
Copied!
1
- (void)viewDidLoad {
2
[super viewDidLoad];
3
self.bannerView = HyprMXBannerView *bannerView = [[HyprMXBannerView alloc] initWithPlacementName:@"Banner" adSize:kHyprMXAdSizeBanner];
4
5
// Add constraints to control the view's size
6
self.bannerView.translatesAutoresizingMaskIntoConstraints = NO;
7
[self.view addSubview:bannerView];
8
[self.bannerView.widthAnchor constraintEqualToConstant:self.bannerView.adSize.width].active = YES;
9
[self.bannerView.heightAnchor constraintEqualToConstant:self.bannerView.adSize.height].active = YES;
10
}
11
Copied!
Example setting the BannerView's Frame:
Swift
Objective-C
1
override func viewDidLoad() {
2
super.viewDidLoad()
3
self.bannerView = HyprMXBannerView.init(placementName: "Banner", adSize: kHyprMXAdSizeBanner)
4
5
// Set the bannerView's frame and add to the UI
6
self.bannerView.frame = CGRect.init(x: 0, y: 0, width: self.bannerView.adSize.width, height: self.bannerView.adSize.height)
7
self.view.addSubview(self.bannerView)
8
}
Copied!
1
- (void)viewDidLoad {
2
[super viewDidLoad];
3
self.bannerView = HyprMXBannerView *bannerView = [[HyprMXBannerView alloc] initWithPlacementName:@"Banner" adSize:kHyprMXAdSizeBanner];
4
5
// Set the bannerView's frame and add to the UI
6
self.bannerView.frame = CGRectMake(0, 0, self.bannerView.adSize.width, self.bannerView.adSize.height);
7
[self.view addSubview:self.bannerView];
8
}
9
10
Copied!
For best visibility, you should horizontally center your HyprMXBannerView in your screen. For example, you can accomplish this using a constraint:
Swift
Objective-C
1
bannerView.centerXAnchor.constraint(equalTo:self.view.safeAreaLayoutGuide.centerXAnchor).isActive = true;
Copied!
1
[bannerView.centerXAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.centerXAnchor].active = YES;
Copied!

Loading Advertisements

To load a banner ad:
Swift
Objective-C
1
bannerView.loadAd()
Copied!
1
[bannerView loadAd];
Copied!
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, you can implement the methods you would like to receive callbacks for.
Swift
Objective-C
1
bannerView.placementDelegate = self
Copied!
1
bannerView.placementDelegate = self
Copied!
NOTE: HyprMX holds a weak reference to the placement delegate, so you must be sure to retain the delegate.
Implement the HyprMXBannerDelegate:
Swift
Objective-C
1
/** Called in response to loadAd when an ad was loaded */
2
func adDidLoad(_ bannerView: HyprMXBannerView) {
3
}
4
5
/** Called in response to loadAd when there was an error loading an ad */
6
func adFailed(toLoad bannerView: HyprMXBannerView, error: Error) {
7
}
8
9
/** Called when the user clicks on the bannerView */
10
func adWasClicked(_ bannerView: HyprMXBannerView) {
11
}
12
13
/** Called when a banner click will open a full-screen modal */
14
func adDidOpen(_ bannerView: HyprMXBannerView) {
15
}
16
17
/** Called when a full-screen modal has been closed */
18
func adDidClose(_ bannerView: HyprMXBannerView) {
19
}
20
21
/** Called when a banner click will open another application */
22
func adWillLeaveApplication(_ bannerView: HyprMXBannerView) {
23
}
Copied!
1
/** Called in response to loadAd when an ad was loaded */
2
-(void)adDidLoad:(HyprMXBannerView *)bannerView {
3
}
4
5
/** Called in response to loadAd when there was an error loading an ad */
6
-(void)adFailedToLoad:(HyprMXBannerView *)bannerView error:(NSError *)error {
7
}
8
9
/** Called when the user clicks on the bannerView */
10
-(void)adWasClicked:(HyprMXBannerView *)bannerView {
11
}
12
13
/** Called when a banner click will open a full-screen modal */
14
-(void)adDidOpen:(HyprMXBannerView *)bannerView {
15
}
16
17
/** Called when a full-screen modal has been closed */
18
-(void)adDidClose:(HyprMXBannerView *)bannerView {
19
}
20
21
/** Called when a banner click will open another application */
22
-(void)adWillLeaveApplication:(HyprMXBannerView *)bannerView {
23
}
24
Copied!

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
1
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
2
super.viewWillTransition(to: size, with: coordinator)
3
coordinator.animate { context in
4
} completion: { context in
5
6
// Update your banner's adSize to the new desired size
7
self.bannerView.adSize = kHyprMXAdSizeLeaderBoard
8
9
// Load a new banner ad
10
self.bannerView.loadAd()
11
}
12
}
Copied!
1
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator {
2
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
3
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
4
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
5
6
// Update your banner's adSize to the new desired size
7
self.bannerView.adSize = kHyprMXAdSizeLeaderBoard;
8
9
// Load a new banner ad
10
[self.bannerView loadAd]
11
}];
12
}
Copied!
Last modified 2mo ago