Show Menu

Looking for an iOS Developer?

Submit your 30 day Job Listing for FREE

In this Google AdMob tutorial, I am going to show you how to display ads in your iOS Applications using the Google SDK for AdMob. A perfect way to make money from apps.

I have had a lot of people recently message in to ask about what is happening with iAds. And what alternatives are there out there so that people don’t loose their revenue stream. I decided to create a quick tutorial to show you how to integrate the Google AdMobs SDK for iOS into your application.

If you need a brush up of Swift, check out our guide to the Swift Programming Language or our Swift Tutorials

In App Advertising

So, what is in App Advertising? Well, you must have seen them around. Almost all free apps have some sort of advertising in them, either banner ads at the bottom or top of the app, or those full screen ads that popup (Interstitial ads). Throughout this how to add advertising to mobile apps tutorial I am going to show you how to implement Banner ads in Swift and how to implement interstitial ads in Swift all using the Google AdMob SDK.

So, let’s go ahead and start. The first thing we are going to do is go and get the latest version of the Google AdMob SDK and download it so that we can use it in this tutorial.

Get the Google AdMob SDK

AdMob requires you to have a valid AdSense account and AdWords account. Throughout this Google AdMob Tutorial you will see codes like: ca-app-pub-3940256099942544/2934735716. These are only test codes provided by Google. When you create your own and link up an app you will get unique ones. These codes are fine for the purpose of this Swift Tutorial.

Go to: https://developers.google.com . You should see this in the middle of the screen:

GoogleAdmob SDK Options

Click the button in the middle iOS. This will then take you to the “Google APIs for iOS” page. Down the page, there are three columns. This page is responsive so the layout might be different we are looking for the section with the title: “Earn”. As shown below:

Earn money with Google AdMob

Click the link that says AdMob.

There are now some new links, Click the one that says download:

AdMob for ios download

This page now has a table of all the available downloads. The one we are using in this AdMob Tutorial is:

google admob stable release

Wait a second, you just made me go all around the Google Admob website when you could have just linked the project form here? Well, yeah I could have, but not for long. Google, like many over service providers are always bringing out updates, version builds as well as site structure changes. It is always a good idea to get a familiar layout of the source site so that you can easily find your way around should you need the file again. So, in that respect.. You’re welcome. :)

Create the Project

In Xcode, create a new Single View Application. Call this whatever you like. Here is the information that I have used.

Google AdMob Tutorial Project

Once you have done that, Xcode will ask you where you want to store the Google AdMob Tutorial project. Chose a location of your desire and save.

Copy the entire folder of the Google AdMob SDK that you just downloaded across into this project folder. I created a folder called “SDKs” as I like to keep things neat and tidy.

Linking Google AdMob to our project

In Xcode, in the project navigation select the name of your project:

GoogleAdMob Project Navigation

Across the top of the Xcode Editor Area, There are now a few sections. Click the one that says Build Phases. As you can see now, there are 4 minimized dropdown panes. Click the one that says Link Binary With Library so that it is open like so:

AdMob Tutorial Link Library

Open a new finder window, go to the location of the Google SDK and drag and drop the GoogleMobileAds.framework into the section above. You now should have:

Linked GoogleAdMobs Framework

There are also some other frameworks that we need to add to the project which the Google AdMob Tutorial Project requires. Whilst still in the Linked Binary with Libraries section click the plus + button at the bottom. This will open up a new dialogue window, use the search at the top of that window to search for the following frameworks and add them:

  • AdSupport.framework
  • AudioToolbox.framework
  • AVFoundation.framework
  • CoreGraphics.framework
  • CoreTelephony.framework
  • EventKit.framework
  • EventKitUI.framework
  • MessageUI.framework
  • StoreKit.framework
  • SystemConfiguration.framework

You should now see this:

GoogleAdMob All Linked Libraries

Click build on your app or CMD + B. I like to do this because some frameworks dont import properly unless Xcode knows the framework is there before you import it.

Import GoogleMobileAds

Now, in your project navigation pane, Click the ViewController.Swift file and at the very top add


import GoogleMobileAds

Now your project is ready to start adding in the ads. Build your project again and save.

Create AdMob Banner Ads

In your project navigation pane, select the Main.storyboard.

In the object Library, search for a UIView:

Xcode Object Library

And drag one onto your storyboard, I dragged mine to the bottom of the storyboard and centered it:

uiview storyboard

Click the UIView that you just added so that it is selected. Then go to the Size Inspector and change the four values to the ones shown below:

UIView Size Inspector

Important Step, Pay Attention!

Now, this is vital. Click the UIView whilst also holding down the CTRL button and drag to the right, inside the UIView:

UIView Width

This will bring up a blue line. When you let go you will see a little dialogue window that will pop up. Click Width at the top:

UIView Link Width

Do the same again, but this time drag the blue line up and click height form the dialogue window.

With the UIView Still Selected, Click the Auto Constraints button (Highlighted red below) Then select “Add Missing Constraints”:

UIView Add Missing Constraints

Again, With the UIView Still Selected, go to the Identity Inspector and change the class to GADBannerView

Google AdMob Tutorial Class-GADBannerView

In your VieController.Swift file create an IBOutlet for your GADBannerView and call it: GoogleBannerView of type: GADBannerView like so:


@IBOutlet weak var GoogleBannerView: GADBannerView!

Link that to the UIView on your storyboard.

Now, in the ViewDidLoad method we are going to load the ad:


GoogleBannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
GoogleBannerView.rootViewController = self
GoogleBannerView.loadRequest(GADRequest())

You might get this error:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file.

This is because you have to set the NSAllowsArbitraryLoads key to YES under NSAppTransportSecurity dictionary in your .plist file.

app transport security

There you have it. Run your app and you should see:

Google AdMob Tutorial Result

Create AdMob Interstitial Ads

So, above I showed you how to display banner ads in swift, now I am going to show you How to implement Interstitial ads.

Interstitial ads are full screen ads that are overlayed on top of an app. They are generally displayed at natural app transition points such as in between game levels. Interstitial ads are a ‘one-time’ only event. You need to reinitialize them every time you want to use. We will get around this by creating a method that will re-initialize it for us.

From the Xcode object library, Drag a UIButton onto your storyboard. Double click and change the text to “Show Interstitial Ad”:

Google AdMob Interstitial Ad

Click the Assistant Editor Button to show the ViewController.Swift file next to your storyboard in a split view. CTRL + drag from the UIButton to the ViewController.Swift file to create a link. This will give you a new dialogue window. Change the connection type to: Action and name it showAd. Then Click Connect:

AdMob Interstitial Ad IBAction

This should add an IBAction into your ViewController.Swift file:


@IBAction func showAd(sender: AnyObject) {

}

We are not going to create a variable property for our AdMob Interstitial Ad just below the class ViewController: UIViewController {:


var interstitialAd : GADInterstitial!

So, remember me saying before about interstitial ads not been re-usable. This is the method that will re-initialize them for us:


func reloadInterstitialAd() -> GADInterstitial {
    var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    interstitial.delegate = self
    interstitial.loadRequest(GADRequest())
    return interstitial
}

Do you get this error?:

Cannot assign value of type ‘ViewController‘ to type ‘GADInterstitialDelegate?

This means that your ViewController does not conform to the type GADInterstitialDelegate, to fix this, change:


class ViewController: UIViewController, UITextFieldDelegate {

to this:


class ViewController: UIViewController, UITextFieldDelegate, GADInterstitialDelegate {

No we are going to use interstitialDidDismissScreen to preload another interstitial immediately after the previous one is dismissed, meaning the app is prepared to show an interstitial ad again at the next logical break point:


func interstitialDidDismissScreen(ad: GADInterstitial!) {
        self.interstitialAd = reloadInterstitialAd()
}

In the ViewDidload, add:


self.interstitialAd = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
let request = GADRequest()
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
self.interstitialAd.loadRequest(request)
self.interstitialAd = reloadInterstitialAd()

Finally, we are not going to make the IBAction that we created earlier actually load an interstitial ad.

We do not want to try and get the interstitial until it has loaded, otherwise we will get all sorts of results, so we are going to be using: self.interstitialAd.isReady


@IBAction func showAd(sender: AnyObject) {
    if self.interstitialAd.isReady {
        self.interstitialAd.presentFromRootViewController(self)
    }
}

tl;dr Download Project Files

Run the app and press the button. Voila.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


iOS-Blog Admin Team

Written by:

We're here to help.

Comments

comments