Get Started

AdMob uses the Google Mobile Ads SDK. The Google Mobile Ads SDK helps app developers gain insights about their users, drives more in-app purchases, and maximizes ad revenue. To do so, the default integration of the Mobile Ads SDK collects information such as device information, publisher-provided location information, and general in-app purchase information (such as item purchase price and currency).

This guide shows you how to create a new iOS project, include the Firebase and Google Mobile Ads SDKs, and make your first banner request.

The ad unit and samples that we provide return test ads. Test ads are always available, even if your account is suspended or disabled. For more information, review the AdMob policies and learn more about invalid activity.

It is against AdMob policy to click on your own live ads. During development and testing, use only test ads. If you do need to render live ads before launch, avoid clicking on them. If you click on live ads, your AdMob account may be suspended.

Prerequisites

  • Xcode 7.0 or higher
  • An iOS 7.0 or higher deployment target
  • A valid Xcode or Firebase project
  • Recommended: installation of CocoaPods to simplify dependency management

Download Banner Example

Add the Firebase and Mobile Ads SDKs

For more detail on how Firebase and AdMob work together and how this impacts existing AdMob publishers, see AdMob with Firebase.

AdMob is part of the Firebase mobile services platform, which uses a file called GoogleService-Info.plist to store configuration information about your app. You need this file to complete this exercise.

The best way to get the file is to log in to the Firebase console and register an app. It's free and just takes a few minutes. When asked for a bundle identifier, enter the Bundle ID from the project you want to use for testing.

The best way to add the Firebase and Mobile Ads SDKs is using CocoaPods, which is described below. (But if you prefer, you can integrate without CocoaPods. Be sure to include everything in the AdMob and Analytics folders.)

Create the Podfile

In the same directory as your BannerExample.xcodeproj file, create a file named Podfile that includes the following:

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '7.0'

target 'BannerExample' do
  pod 'Firebase/Core'
  pod 'Firebase/AdMob'
end

Run pod update

Run pod update from the terminal, in the same directory as the Podfile, to be sure you get the latest iOS SDK into your app and that all the APIs are there. Once the installation finishes, close BannerExample.xcodeproj and open up BannerExample.xcworkspace. Your project files should include a Pods project with new dependencies for Firebase and AdMob.

Rebuild your project

Rebuild and run your project. The app still shows a white screen, but now the Google Mobile Ads SDK is being referenced in your app. You can now move on to making your first banner request.

Your first banner request

Now that you have a project with the SDK referenced, it's time to put banner ads into it.

A GADBannerView can be created from storyboard or from code. Since layouts are generally defined in storyboard, this guide shows the storyboard method.

Add a GADBannerView in storyboard

Click the Main.storyboard tab. In the bottom-right corner, select a UIView element and drag it into your view controller. Then in the Custom Class section in the top-right corner, select the custom class GADBannerView as the Class for this view.

Add constraints on the GADBannerView

Set the following constraints on the GADBannerView:

  • Locate it at the bottom of the screen.
  • Set the size of the view to 320 wide by 50 high.
  • Center it.

Make sure the view is selected, and click the Pin icon at the bottom of the screen. Add a Spacing to nearest neighbor constraint on the bottom of the banner with the value of 0. This pins the view to the bottom of the screen.

Also check the width and height constraints, and set the values to 320 and 50, respectively, to set the size of the view.

Click the Align icon to the left of the Pin icon, and add a constraint for Horizontally in Container with a value of 0. This orients the banner to display horizontally across the bottom of the screen.

To see the positioning of your view after making changes to constraints, select the Resolve Auto Layout Issues icon to the right of the Pin icon, and select Update Frames.

The banner is now correctly positioned as shown by the bounding box at the bottom of the following screen.

Adding a reference to your GADBannerView in code

The GADBannerView needs a reference in code to load ads into it. Open up the Assistant Editor by navigating to View > Assistant Editor > Show Assistant Editor. Make sure the ViewController.h file is showing in the Assistant Editor (the right pane of the screen). Next, holding the control key, click the GADBannerView (in the center pane), and drag your cursor over to ViewController.h (as indicated by the blue line going from the center pane to the right pane).

For a Swift project, follow the steps above, but add a reference to the GADBannerView in the ViewController.swift file. Xcode generates and connects a property for you. Name it "bannerView", and select Connect.

To resolve a compilation error, add @import GoogleMobileAds to ViewController.h or import GoogleMobileAds to ViewController.swift so the compiler knows that GADBannerView is a valid class.

Objective-C

ViewController.h
#import <UIKit/UIKit.h>

@import GoogleMobileAds;

@interface ViewController : UIViewController

@property (nonatomic, weak) IBOutlet GADBannerView  *bannerView;

@end

Swift

ViewController.swift
import UIKit
import GoogleMobileAds

class ViewController: UIViewController {

  @IBOutlet weak var bannerView: GADBannerView!

} 

Initialize the Google Mobile Ads SDK

At app launch, initialize the Google Mobile Ads SDK by calling configureWithApplicationID: in the application:didFinishLaunchingWithOptions: method of AppDelegate.m or AppDelegate.swift.

Objective-C

AppDelegate.m
- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  // Use Firebase library to configure APIs
  [FIRApp configure];
  [GADMobileAds configureWithApplicationID:@"ca-app-pub-3940256099942544~1458002511"];
  return YES;
}

Swift

AppDelegate.swift
func application(_ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

  // Use Firebase library to configure APIs
  FIRApp.configure()
  GADMobileAds.configure(withApplicationID: "ca-app-pub-3940256099942544~1458002511")
  return true
}

Initializing the Google Mobile Ads SDK at app launch allows the SDK to fetch app-level settings and perform configuration tasks as early as possible. This can help reduce latency for the initial ad request. Initialization requires an app ID. App IDs are unique identifiers given to mobile apps when they're registered in the AdMob console.

To find your app ID, click the App management option under the settings dropdown (located in the upper right hand corner) on the AdMob account page. App IDs have the form ca-app-pub-XXXXXXXXXXXXXXXX~NNNNNNNNNN.

Load an ad into GADBannerView

Finally, add code into ViewController.m or ViewController.swift that loads an ad into the banner view.

Objective-C

ViewController.m
- (void)viewDidLoad {
  [super viewDidLoad];

  NSLog(@"Google Mobile Ads SDK version: %@", [GADRequest sdkVersion]);
  self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
  self.bannerView.rootViewController = self;
  [self.bannerView loadRequest:[GADRequest request]];
}

Swift

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

  print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
  bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
  bannerView.rootViewController = self
  bannerView.load(GADRequest())
}

To specify the ad to load, make these three changes:

  1. In the viewDidLoad method, set an ad unit ID on the banner. For convenience, you can use the test ad unit ID provided above for now. But eventually you must create an ad unit ID, from the AdMob interface to use in your app. When first integrating your app with AdMob, be sure to use the provided test ad unit ID or see Test ads to designate a test device.

    It is against AdMob policy to click on your own live ads. During development and testing, use only test ads. If you do need to render live ads before launch, avoid clicking on them. If you click on live ads, your AdMob account may be suspended.

  2. Set the root view controller to be the view controller that holds GADBannerView. This view controller is used to present an overlay when the ad is clicked.

  3. Call loadRequest: on GADBannerView with a GADRequest object.

Build and run the app

Build and run the app. This time, a test banner ad is displayed at the bottom of the screen.

Congratulations, you've successfully made your first ad request!

See the finished example on GitHub

Objective-C

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // Replace this ad unit ID with your own ad unit ID.
  self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
  self.bannerView.rootViewController = self;

  GADRequest *request = [GADRequest request];
  // Requests test ads on devices you specify. Your test device ID is printed to the console when
  // an ad request is made. GADBannerView automatically returns test ads when running on a
  // simulator.
  request.testDevices = @[
    @"2077ef9a63d2b398840261c8221a0c9a"  // Eric's iPod Touch
  ];
  [self.bannerView loadRequest:request];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

@end

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  /// The banner view.
  @IBOutlet weak var bannerView: GADBannerView!

  override func viewDidLoad() {
    super.viewDidLoad()
    print("Google Mobile Ads SDK version: \(GADRequest.sdkVersion())")
    bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
    bannerView.rootViewController = self
    bannerView.load(GADRequest())
  }

}

See the complete implementation of the banner example on GitHub:

Objective-C Swift

What's next

To do more with AdMob, sign up for an AdMob account.

To see how to get Firebase and AdMob working together, check out the video to the right.

To fine-tune your banner implementation, check out the following guides:

To learn about full-screen interstitial ads, check out Interstitial Ads.

FAQ

I'm using CocoaPods. How do I update the SDK?
In the terminal, run pod update in the directory where the Podfile is located.
Do I need to use test ads?
Yes. It is against AdMob policy to click on your own live ads. During development and testing, use only test ads. If you do need to render live ads before launch, avoid clicking on them. If you click on live ads, your AdMob account may be suspended. For more information, review the AdMob policies and learn more about invalid activity.
How do I get test ads on my physical device?
See Test ads for instructions on how to get your test device ID.
How do I get an AdMob ad unit ID?
Create an ad unit, being careful to remember that AdMob ad unit IDs have the form ca-app-pub-XXXXXXXXXXXXXXXX/NNNNNNNNNN.
I keep getting the error "Invalid unknown request error: Cannot determine request type. Is your ad unit id correct?"
Make sure your ad unit ID is correct. It must be in the form ca-app-pub-XXXXXXXXXXXXXXXX/NNNNNNNNNN. Using other forms, such as pub-XXXXXXXXXXXXXXXX, causes this error.

Send feedback about...

AdMob by Google
AdMob by Google