Skip to main content
Version: v1.1

Banner ads

Prerequisites

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:

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)
])
}

}

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:

- (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:

override func viewDidLoad() {
super.viewDidLoad()
...

bannerView.adUnitID = "/361045431/example/banner"
bannerView.rootViewController = self
bannerView.load(GAMRequest())
}

GAMRequest objects represent a single ad request, and contain properties for things like targeting information.

Note: If your ad fails to load, you don't need to explicitly request another one as long as you've configured your ad unit to refresh; the Google Mobile Ads SDK respects any refresh rate you specified in the Ad Manager UI. If you haven't enabled refresh, you will need to issue a new request.

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.

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADBannerViewDelegate {

var bannerView: GAMBannerView!

override func viewDidLoad() {
super.viewDidLoad()
...
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:

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")
}

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 methodiOS method
bannerViewWillPresentScreen:UIViewController's viewWillDisappear:
bannerViewWillDismissScreen:UIViewController's viewWillAppear:
bannerViewDidDismissScreen:UIViewController's viewDidAppear:

The table below lists the standard banner sizes.

Size in points (WxH)DescriptionAvailabilityAdSize constant
320x50BannerPhones and tabletsGADAdSizeBanner
320x100Large bannerPhones and tabletsGADAdSizeLargeBanner
300x250IAB medium rectanglePhones and tabletsGADAdSizeMediumRectangle
468x60IAB full-size bannerTabletsGADAdSizeFullBanner
728x90IAB leaderboardTabletsGADAdSizeLeaderboard
Provided width x Adaptive heightAdaptive bannerPhones and TabletsN/A