Banner ads occupy a spot within an app's layout, either at the top or bottom of the device screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you're new to mobile advertising, they're a great place to start. Case study.
This guide shows you how to integrate banner ads from AdMob into an iOS app. In addition to code snippets and instructions, it includes information about sizing banners properly and links to additional resources.
Prerequisites
- Complete the Get started guide.
Always test with test ads
When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
The easiest way to load test ads is to use our dedicated test ad unit ID
for iOS banners:
ca-app-pub-3940256099942544/2934735716
It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.
For more information about how the Mobile Ads SDK's test ads work, see Test Ads.
Create a GADBannerView
Banner ads are displayed in
GADBannerView
objects, so the first step toward integrating banner ads is to include a
GADBannerView
in your view hierarchy. This is
typically done either with the Interface Builder or programmatically.
Interface Builder
A GADBannerView
can be added to a storyboard or xib
file like any typical view. When using this method, be 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 GADBannerView
can also be instantiated directly.
Here's an example of how to create a GADBannerView
,
aligned to the bottom center of the safe area of the screen, with a
banner size of 320x50:
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController { var bannerView: GADBannerView! override func viewDidLoad() { super.viewDidLoad() // In this case, we instantiate the banner with desired ad size. bannerView = GADBannerView(adSize: GADAdSizeBanner) addBannerViewToView(bannerView) } func addBannerViewToView(_ bannerView: GADBannerView) { bannerView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(bannerView) view.addConstraints( [NSLayoutConstraint(item: bannerView, attribute: .bottom, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0), NSLayoutConstraint(item: bannerView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0) ]) } }
Objective-C
@import GoogleMobileAds; @interface ViewController () @property(nonatomic, strong) GADBannerView *bannerView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // In this case, we instantiate the banner with desired ad size. self.bannerView = [[GADBannerView 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.view.safeAreaLayoutGuide attribute:NSLayoutAttributeBottom 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.
If you don't want to use a standard size defined by a constant, you can set
a custom size using
GADAdSizeFromCGSize
.
See the banner size section for more information.
Configure GADBannerView properties
In order to load and display ads, GADBannerView
requires a few properties 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 theGADBannerView
.adUnitID
- This is the ad unit ID from which theGADBannerView
should load ads.
Here's a code example showing how to set the two required properties in the
viewDidLoad
method of a UIViewController:
Swift
override func viewDidLoad() { super.viewDidLoad() ... bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716" bannerView.rootViewController = self }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; ... self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716"; self.bannerView.rootViewController = self; }
Load an ad
Once the GADBannerView
is in place and its properties
configured, it's time to load an ad. This is done by calling loadRequest:
on a
GADRequest
object:
Swift
override func viewDidLoad() { super.viewDidLoad() ... bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716" bannerView.rootViewController = self bannerView.load(GADRequest()) }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; ... self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716"; self.bannerView.rootViewController = self; [self.bannerView loadRequest:[GADRequest request]]; }
GADRequest objects represent a single ad request, and contain properties for things like targeting information.
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 AdMob 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
GADBannerView
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
import GoogleMobileAds import UIKit class ViewController: UIViewController, GADBannerViewDelegate { var bannerView: GADBannerView! override func viewDidLoad() { super.viewDidLoad() ... bannerView.delegate = self } }
Objective-C
@import GoogleMobileAds; @interface ViewController () <GADBannerViewDelegate> @property(nonatomic, strong) GADBannerView *bannerView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; ... self.bannerView.delegate = self; }
Implementing 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
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") }
Objective-C
- (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"); }
See the Ad Delegate example for an implementation of banner delegate methods in the iOS API Demo app.
Use cases
Here are some example use cases for these ad event methods.
Adding a banner to the view hierarchy once an ad is received
You may want to delay in adding a GADBannerView
to the view hierarchy until
after an ad is received. You can do this by listening for the
bannerViewDidReceiveAd:
event:
Swift
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) { // Add banner to view and add constraints as above. addBannerViewToView(bannerView) }
Objective-C
- (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView { // Add bannerView to view and add constraints as above. [self addBannerViewToView:self.bannerView]; }
Animating a banner ad
You can also use the bannerViewDidReceiveAd:
event to animate a banner ad
once it's returned, as shown in the following example:
Swift
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) { bannerView.alpha = 0 UIView.animate(withDuration: 1, animations: { bannerView.alpha = 1 }) }
Objective-C
- (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView { bannerView.alpha = 0; [UIView animateWithDuration:1.0 animations:^{ bannerView.alpha = 1; }]; }
Pausing and resuming 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 as
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 |
Custom ad sizes
To define a custom banner size, set your desired size using
GADAdSizeFromCGSize
, as shown
here:
Swift
let adSize = GADAdSizeFromCGSize(CGSize(width: 300, height: 50))
Objective-C
GADAdSize size = GADAdSizeFromCGSize(CGSizeMake(300, 50));
Additional resources
Examples on GitHub
Banner ads example: Swift | Objective-C
Advanced features demo: Swift | Objective-C
Banner UITableView example on GitHub: Swift | Objective-C
Mobile Ads Garage video tutorials
Success stories
Next steps
Learn more about user privacy.