Get Started

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

Prerequisites

Download DFP Banner Example

Creating a new project

In this step, we'll create a brand new project in Xcode. If you don't already have Xcode running, go ahead and open it now.

Create a new Xcode project

Navigate to File > New > Project. Select Single View Application under iOS applications, and click Next.

Name your project

Give your project the name "DFPBannerExample". Choose either "Swift" or "Objective-C" for the language. Then click Next.

Select a project location

Select a location for your project, and click Create to finish creating a new project.

Build and run your new project

To use a simulator, navigate to Product > Destination and select an iPhone simulator. Then select Product > Run to verify your app builds and runs. The app shows just a blank white screen for now. But don't worry, we'll add content in the next steps.

Adding the SDK to your Xcode project

There are two ways to add the Google Mobile Ads SDK to your Xcode project:

Streamlined, using CocoaPods

Create the Podfile

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

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

platform :ios, '7.0'

target 'DFPBannerExample' do
  pod 'Google-Mobile-Ads-SDK', '~> 7.8'
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 DFPBannerExample.xcodeproj and open up DFPBannerExample.xcworkspace.

Your project files should include a Pods project with the Pods/Google-Mobile-Ads-SDK directory.

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.

Manually, using the SDK download

If you don't already have the Google Mobile Ads SDK, grab it from the downloads page and unzip it.

Add the framework

Right-click on the DFPBannerExample project, and choose Add Files To "DFPBannerExample".

Add the GoogleMobileAds.framework.

Rebuild your project

Rebuild and run your project. You'll still see a white screen, but now you'll see a log in the Xcode console indicating what version of the Google Mobile Ads SDK you have.

Your first banner request

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

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

Add a DFPBannerView in storyboard

Open Main.storyboard. In the Object library in the bottom right corner, search for UIView, and drag a UIView element into your view controller. Then in the Identity inspector in the top right corner, give this view the custom class DFPBannerView.

Add constraints on the DFPBannerView

We'll set constraints on the DFPBannerView to center it at the bottom of the "Safe Area" of the screen , and have a size of 320x50.

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

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

Next, click the Align icon to the left of the Pin icon, and add a constraint for Horizontal Center in Container with a value of 0.

After making changes to constraints, you can see where your view will be positioned by selecting the Resolve Auto Layout Issues icon to the right of Pin and selecting Update frames.

The banner will now be correctly positioned.

Adding a reference to your DFPBannerView in code

The DFPBannerView needs a reference in code to load ads into it. Open up the Assistant Editor by navigating to View > Assistant Editor > Show Assistant Editor. In the assistant editor, make sure the ViewController.h file is showing. Next, holding the control key, click the DFPBannerView and drag your cursor over to ViewController.swift. For an Objective-C project, follow the steps above, but add a reference to the DFPBannerView in the ViewController.h file.

Xcode will generate and connect a property for you. Name it "bannerView", and select Connect.

Swift

ViewController.swift
import UIKit
import GoogleMobileAds

class ViewController: UIViewController {

  @IBOutlet weak var bannerView: DFPBannerView!

} 

Objective-C

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

@import GoogleMobileAds;

@interface ViewController : UIViewController

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

@end

To resolve a compilation error, also include @import GoogleMobileAds in ViewController.h or import GoogleMobileAds in ViewController.swift so the compiler knows that DFPBannerView is a valid class.

Load an ad into DFPBannerView

Finally, make these changes:

  1. Set an ad unit ID on the banner. You'll eventually need to traffic your own line item in the DFP UI, target that line item with an ad unit, and use that ad unit ID in your app. For convenience, though, you can use the sample ad unit ID provided above which is trafficked to a test line item.

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

  3. Call loadRequest: on DFPBannerView with a DFPRequest object.

These three things are achieved in the following code snippet:

Swift

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

  print("Google Mobile Ads SDK version: \(DFPRequest.sdkVersion())")
  
  bannerView.adUnitID = "/6499/example/banner"
  bannerView.rootViewController = self
  bannerView.load(DFPRequest())
}

Objective-C

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

  NSLog(@"Google Mobile Ads SDK version: %@", [DFPRequest sdkVersion]);
  
  self.bannerView.adUnitID = @"/6499/example/banner";
  self.bannerView.rootViewController = self;
  [self.bannerView loadRequest:[DFPRequest request]];
}

Running the app

Build and run the app. This time, you'll see a test banner ad at the bottom of the Safe Area.

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

See the finished example on GitHub

Swift

Objective-C

See the complete implementation of the banner example on GitHub:

Objective-C Swift

What's next

To learn about full-screen interstitial ads, check out the Interstitial Ad guide.

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

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.
How do I get a DFP ad unit ID?
The format for the DFP ad unit ID is: /networkCode/adUnitName. You can generate the ad unit ID in DFP. First, sign in to your DFP account. On the Inventory tab, navigate to the ad unit, and click Generate tags. Alternatively, you can find the network code for your account on the Admin tab under Network Settings.
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 should be of the form /<network_code>/<ad_unit_path>.

Send feedback about...

SDK for DFP Users on iOS
Need help? Visit our support page.