So you want to implement ads in your shiny new app? Or maybe you are working on some project in your company, and you need ads in that project? In any case, the best way (IMHO) is to go with Google services for ads (this post will deal with Ad Exchange, but if you are targeting firebase / AdSense, the process is basically the same).

Although Ad Exchange and AdSense looks like the same thing (at least for me as a developer who is looking only from the technical side of the service), there is some big difference when it comes to those two services. Ad Exchange is more like premium option for bigger businesses that offers more premium features like anonymity, ability to set up preferred deals, way more filtering and blocking options than AdSense, etc…

In this post, we will assume that you (as a reader) undersatnd the field around iOS development and you are familiar with terms such cocoa pods. Also we will assume that you already have configured your Ad Exchange account, already have your ad unit id configured and set up.

Let’s begin with banner ads (just because I think they are the best kind of ads).

First of all, we need to install Google Mobile Ads SDK, the preferred way to do this is by installing them trough cocoa pods, or you can do it manually by downloading the SDK. After that copy it to your project.

If you chose to go with the cocoa pods, add this line:

pod 'Google-Mobile-Ads-SDK'

to your podfile, and install the SDK by running ‘pod install’.

Now, that your SDK in installed and ready, comes the fun part.

First we need to determine the dimensions of your banner ad.

We will go with the first option, the 320×50 dimension ad that will perfectly suit our needs.

The first thing to do now is to create the ad view, the easiest way to accomplish this is trough storyboards. So open your storyboard, create small view at the bottom of your screen with dimensions of 320×50 (or whichever dimension you’ve chosen), constrain it and, in the identity inspector, give it the class name of GADBannerView.

Now the obvious part, create the IBOutlet from this view to the ViewController, name it something like “bannerAdView”.

Finally, add code for configuring and loading the ads. Configuration is quite straightforward and easy. But first, be sure to import the SDK in your view controller like:

import GoogleMobileAds

Now create request, but be sure to use test ad unit id, or designate a test device (in our case this is the simulator):

let req = GADRequest()
req.testDevices = [kGADSimulatorID]

Now add some information to your bannerAdview like adSize, root view controller and ad unit id:

self.bannerAdView.adSize = kGADAdSizeBanner
self.bannerAdView.rootViewController = self
self.bannerAdView.adUnitID = "ca-mb-app-pub-YOUR_APP_ID_NUMBER"

and load the request:

self.bannerAdView.loadRequest(req)

So your final code should look something like:

import GoogleMobileAds
final class ViewController: UIViewController {
@IBOutlet weak var bannerAdView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
let req = GADRequest()
req.testDevices = [kGADSimulatorID]
self.bannerAdView.adSize = kGADAdSizeBanner
self.bannerAdView.rootViewController = self
self.bannerAdView.adUnitID = "ca-mb-app-pub-YOUR_APP_ID_NUMBER"
self.bannerAdView.loadRequest(req)
}
}

 

You can also take advantage of GADBannerViewDelegate and gain more control over ads.

But that’s not all, if you need more information, take look at the official documentation, there you can find more information about other kind of ads like Interstitial (full screen) ads, or native ads.