Skip to main content
Version: v2.0

Banner ads

Prerequisites

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.

In the current Google Mobile Ads SDK for iOS (12.x), the Swift API drops the 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:

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

}

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:

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

premiumAdsBannerView.adUnitID = "/361045431/example/banner"
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:

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

premiumAdsBannerView.adUnitID = "/361045431/example/banner"
premiumAdsBannerView.rootViewController = self
premiumAdsBannerView.load(AdManagerRequest())
}

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

For testing during development you can use Google's sample banner ad unit ID ca-app-pub-3940256099942544/2435281174 with the AdMob BannerView / GADBannerView. For Google Ad Manager testing, refer to Google's Ad Manager sample ad units.
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 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.

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, BannerViewDelegate {

var premiumAdsBannerView: AdManagerBannerView!

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

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

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 methodiOS method
bannerViewWillPresentScreenUIViewController.viewWillDisappear
bannerViewWillDismissScreenUIViewController.viewWillAppear
bannerViewDidDismissScreenUIViewController.viewDidAppear

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)DescriptionAvailabilityAdSize constant (Swift / Objective-C)
320x50BannerPhones and tabletsAdSizeBanner / GADAdSizeBanner
320x100Large bannerPhones and tabletsAdSizeLargeBanner / GADAdSizeLargeBanner
300x250IAB medium rectanglePhones and tabletsAdSizeMediumRectangle / GADAdSizeMediumRectangle
468x60IAB full-size bannerTabletsAdSizeFullBanner / GADAdSizeFullBanner
728x90IAB leaderboardTabletsAdSizeLeaderboard / GADAdSizeLeaderboard
Provided width x Adaptive heightAdaptive bannerPhones and tabletsN/A