Banner ads
Prerequisites
- Complete the Get started guide.
Create a GAMBannerView
Banner ads are displayed in GAMBannerView objects, so the first step toward integrating banner ads is to include a GAMBannerView in your view hierarchy. This is typically done either with the Interface Builder or programmatically.
Interface Builder
A GAMBannerView 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 GAMBannerView can also be instantiated directly. Here's an example of how to create a GAMBannerView, 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 bannerView: GAMBannerView!
override func viewDidLoad() {
super.viewDidLoad()
// In this case, we instantiate the banner with desired ad size.
bannerView = GAMBannerView(adSize: GADAdSizeBanner)
addBannerViewToView(bannerView)
}
func addBannerViewToView(_ bannerView: GAMBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}
}
@import GoogleMobileAds;
@interface ViewController ()
@property(nonatomic, strong) GAMBannerView *bannerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// In this case, we instantiate the banner with desired ad size.
self.bannerView = [[GAMBannerView alloc]
initWithAdSize:GADAdSizeBanner];
[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.bottomLayoutGuide
attribute:NSLayoutAttributeTop
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]
]];
}
@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 GAMBannerView properties
In order to load and display ads, GAMBannerView 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 GAMBannerView.
- adUnitID - This is the ad unit ID from which the GAMBannerView 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
- (void)viewDidLoad {
[super viewDidLoad];
...
self.bannerView.adUnitID = @"/361045431/example/banner";
self.bannerView.rootViewController = self;
}
- (void)viewDidLoad {
[super viewDidLoad];
...
self.bannerView.adUnitID = @"/361045431/example/banner";
self.bannerView.rootViewController = self;
}
Load an ad
Once the GAMBannerView is in place and its properties configured, it's time to load an ad. This is done by calling loadRequest: on a GAMRequest object:
- Swift
- Objective-C
override func viewDidLoad() {
super.viewDidLoad()
...
bannerView.adUnitID = "/361045431/example/banner"
bannerView.rootViewController = self
bannerView.load(GAMRequest())
}
- (void)viewDidLoad {
[super viewDidLoad];
...
self.bannerView.adUnitID = @"/361045431/example/banner";
self.bannerView.rootViewController = self;
[self.bannerView loadRequest:[GAMRequest request]];
}
GAMRequest objects represent a single ad request, and contain properties for things like targeting information.
Ad events
Through the use of GADBannerViewDelegate, 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 GAMBannerView to an object that implements the GADBannerViewDelegate 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, GADBannerViewDelegate {
var bannerView: GAMBannerView!
override func viewDidLoad() {
super.viewDidLoad()
...
bannerView.delegate = self
}
}
@import GoogleMobileAds;
@interface ViewController () <GADBannerViewDelegate>
@property(nonatomic, strong) GAMBannerView *bannerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
...
self.bannerView.delegate = self;
}
Implement banner events
Each of the methods in GADBannerViewDelegate 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: GADBannerView) {
print("bannerViewDidReceiveAd")
}
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
print("bannerView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
func bannerViewDidRecordImpression(_ bannerView: GADBannerView) {
print("bannerViewDidRecordImpression")
}
func bannerViewWillPresentScreen(_ bannerView: GADBannerView) {
print("bannerViewWillPresentScreen")
}
func bannerViewWillDismissScreen(_ bannerView: GADBannerView) {
print("bannerViewWillDIsmissScreen")
}
func bannerViewDidDismissScreen(_ bannerView: GADBannerView) {
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 GADBannerViewDelegate 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 GADBannerViewDelegate 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 GADBannerViewDelegate methods:
| GADBannerViewDelegate method | iOS method |
|---|---|
| bannerViewWillPresentScreen: | UIViewController's viewWillDisappear: |
| bannerViewWillDismissScreen: | UIViewController's viewWillAppear: |
| bannerViewDidDismissScreen: | UIViewController's viewDidAppear: |
Banner sizes
The table below lists the standard banner sizes.
| Size in points (WxH) | Description | Availability | AdSize constant |
|---|---|---|---|
| 320x50 | Banner | Phones and tablets | GADAdSizeBanner |
| 320x100 | Large banner | Phones and tablets | GADAdSizeLargeBanner |
| 300x250 | IAB medium rectangle | Phones and tablets | GADAdSizeMediumRectangle |
| 468x60 | IAB full-size banner | Tablets | GADAdSizeFullBanner |
| 728x90 | IAB leaderboard | Tablets | GADAdSizeLeaderboard |
| Provided width x Adaptive height | Adaptive banner | Phones and Tablets | N/A |