Banner ads
Prerequisites
- Complete the Get started guide.
Create a Banner Ad View
Banner ads are displayed in Google Ad Manager banner-view objects (AdManagerBannerView in Swift, GAMBannerView in Objective-C), so the first step toward integrating banner ads is to include one in your view hierarchy. This is typically done either with the Interface Builder or programmatically.
GAM / GAD prefixes. GAMBannerView is exposed in Swift as AdManagerBannerView, and GADAdSizeBanner is exposed as AdSizeBanner. The Objective-C class names continue to use the GAM / GAD prefix.Interface Builder
A banner-view can be added to a storyboard or xib file like any typical view. When using this method, make sure to add width and height constraints to match the ad size you'd like to display. For example, when displaying a banner (320x50), use a width constraint of 320 points, and a height constraint of 50 points.
Programmatically
A banner-view can also be instantiated directly. Here's an example of how to create one, aligned to the bottom center of the safe area of the screen, with a banner size of 320x50:
- Swift
- Objective-C
import GoogleMobileAds
import UIKit
class ViewController: UIViewController {
var premiumAdsBannerView: AdManagerBannerView!
override func viewDidLoad() {
super.viewDidLoad()
// In this case, we instantiate the banner with desired ad size.
premiumAdsBannerView = AdManagerBannerView(adSize: AdSizeBanner)
addBannerViewToView(premiumAdsBannerView)
}
func addBannerViewToView(_ bannerView: AdManagerBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
NSLayoutConstraint.activate([
bannerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
bannerView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
}
@import GoogleMobileAds;
@interface ViewController ()
@property(nonatomic, strong) GAMBannerView *premiumAdsBannerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// In this case, we instantiate the banner with desired ad size.
self.premiumAdsBannerView = [[GAMBannerView alloc]
initWithAdSize:GADAdSizeBanner];
[self addBannerViewToView:self.premiumAdsBannerView];
}
- (void)addBannerViewToView:(UIView *)bannerView {
bannerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bannerView];
[NSLayoutConstraint activateConstraints:@[
[bannerView.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor],
[bannerView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor]
]];
}
@end
Note that in this case, we don't give width or height constraints, as the provided ad size will give the banner an intrinsic content size to size the view.
Configure banner-view properties
In order to load and display ads, the banner-view requires a few properties to be set.
- rootViewController - This view controller is used to present an overlay when the ad is clicked. It should normally be set to the view controller that contains the banner-view.
- adUnitID - This is the ad unit ID from which the banner-view should load ads.
Here's a code example showing how to set the two required properties in the viewDidLoad method of a UIViewController:
- Swift
- Objective-C
override func viewDidLoad() {
super.viewDidLoad()
...
premiumAdsBannerView.adUnitID = "/361045431/example/banner"
premiumAdsBannerView.rootViewController = self
}
- (void)viewDidLoad {
[super viewDidLoad];
...
self.premiumAdsBannerView.adUnitID = @"/361045431/example/banner";
self.premiumAdsBannerView.rootViewController = self;
}
Load an ad
Once the banner-view is in place and its properties configured, it's time to load an ad. This is done by calling load(_:) (Swift) / loadRequest: (Objective-C) with an AdManagerRequest / GAMRequest object:
- Swift
- Objective-C
override func viewDidLoad() {
super.viewDidLoad()
...
premiumAdsBannerView.adUnitID = "/361045431/example/banner"
premiumAdsBannerView.rootViewController = self
premiumAdsBannerView.load(AdManagerRequest())
}
- (void)viewDidLoad {
[super viewDidLoad];
...
self.premiumAdsBannerView.adUnitID = @"/361045431/example/banner";
self.premiumAdsBannerView.rootViewController = self;
[self.premiumAdsBannerView loadRequest:[GAMRequest request]];
}
Ad-request objects represent a single ad request, and contain properties for things like targeting information.
ca-app-pub-3940256099942544/2435281174 with the AdMob BannerView / GADBannerView. For Google Ad Manager testing, refer to Google's Ad Manager sample ad units.Ad events
Through the use of the banner-view delegate (BannerViewDelegate in Swift, GADBannerViewDelegate in Objective-C), you can listen for lifecycle events, such as when an ad is closed or the user leaves the app.
Registering for banner events
To register for banner ad events, set the delegate property on the banner-view to an object that implements the delegate protocol. Generally, the class that implements banner ads also acts as the delegate class, in which case the delegate property can be set to self.
- Swift
- Objective-C
import GoogleMobileAds
import UIKit
class ViewController: UIViewController, BannerViewDelegate {
var premiumAdsBannerView: AdManagerBannerView!
override func viewDidLoad() {
super.viewDidLoad()
...
premiumAdsBannerView.delegate = self
}
}
@import GoogleMobileAds;
@interface ViewController () <GADBannerViewDelegate>
@property(nonatomic, strong) GAMBannerView *premiumAdsBannerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
...
self.premiumAdsBannerView.delegate = self;
}
Implement banner events
Each of the delegate methods is marked as optional, so you only need to implement the methods you want. This example implements each method and logs a message to the console:
- Swift
- Objective-C
func bannerViewDidReceiveAd(_ bannerView: BannerView) {
print("bannerViewDidReceiveAd")
}
func bannerView(_ bannerView: BannerView, didFailToReceiveAdWithError error: Error) {
print("bannerView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
func bannerViewDidRecordImpression(_ bannerView: BannerView) {
print("bannerViewDidRecordImpression")
}
func bannerViewWillPresentScreen(_ bannerView: BannerView) {
print("bannerViewWillPresentScreen")
}
func bannerViewWillDismissScreen(_ bannerView: BannerView) {
print("bannerViewWillDismissScreen")
}
func bannerViewDidDismissScreen(_ bannerView: BannerView) {
print("bannerViewDidDismissScreen")
}
- (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView {
NSLog(@"bannerViewDidReceiveAd");
}
- (void)bannerView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(NSError *)error {
NSLog(@"bannerView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}
- (void)bannerViewDidRecordImpression:(GADBannerView *)bannerView {
NSLog(@"bannerViewDidRecordImpression");
}
- (void)bannerViewWillPresentScreen:(GADBannerView *)bannerView {
NSLog(@"bannerViewWillPresentScreen");
}
- (void)bannerViewWillDismissScreen:(GADBannerView *)bannerView {
NSLog(@"bannerViewWillDismissScreen");
}
- (void)bannerViewDidDismissScreen:(GADBannerView *)bannerView {
NSLog(@"bannerViewDidDismissScreen");
}
Pause and resume the app
The banner-view delegate protocol has methods to notify you of events, such as when a click causes an overlay to be presented or dismissed. If you want to trace whether these events were due to ads, register for these delegate methods.
To catch all types of overlay presentations or external browser invocations, not just those that come from ad clicks, your app is better off listening for the equivalent methods on UIViewController or UIApplication. Here is a table showing the equivalent iOS methods that are invoked at the same time with delegate methods:
| Banner delegate method | iOS method |
|---|---|
bannerViewWillPresentScreen | UIViewController.viewWillDisappear |
bannerViewWillDismissScreen | UIViewController.viewWillAppear |
bannerViewDidDismissScreen | UIViewController.viewDidAppear |
Banner sizes
The table below lists the standard banner sizes. In Swift the constants drop the GAD prefix; the Objective-C names remain unchanged.
| Size in points (WxH) | Description | Availability | AdSize constant (Swift / Objective-C) |
|---|---|---|---|
| 320x50 | Banner | Phones and tablets | AdSizeBanner / GADAdSizeBanner |
| 320x100 | Large banner | Phones and tablets | AdSizeLargeBanner / GADAdSizeLargeBanner |
| 300x250 | IAB medium rectangle | Phones and tablets | AdSizeMediumRectangle / GADAdSizeMediumRectangle |
| 468x60 | IAB full-size banner | Tablets | AdSizeFullBanner / GADAdSizeFullBanner |
| 728x90 | IAB leaderboard | Tablets | AdSizeLeaderboard / GADAdSizeLeaderboard |
| Provided width x Adaptive height | Adaptive banner | Phones and tablets | N/A |