Hey, everybody.
I'm Andrew Brogdon, and welcome back to the Mobile Ads Garage,
where the videos are like my mother's Christmas cookies,
chock full of ginger.
Today we are all about interstitials,
which is a word I'm still not sure how to spell.
Fortunately, Gary the graphics guy has no such problems.
Nice work, Gar.
All right.
We're going to cover the basics of how interstitial ads fit
into apps, and then we'll code up a sample implementation
in iOS and Android.
Now, most of you are probably familiar with banner ads.
They occupy a little spot in a viewer activity
and stay visible while the user interacts
with the rest of the UI.
Interstitials, on the other hand,
appear briefly in between user interactions
and they use all of a device's screen space.
So instead of this, the user sees something like this.
That's an interstitial.
The user can spend a second or two considering the ad's
message, and either tap on the ad to click through,
or use the Close button at the top to return to the app.
So that's the basic idea.
Now, one other thing I want to talk about
before we get to implementation is
how requests for interstitials work.
Requests for banners are synchronous.
That means the usual pattern goes like this.
You create an ad view.
You call its method to load an ad.
And then when the response comes from the server,
it's immediately displayed in the ad view.
Interstitials are a little different.
Because they cover the entire UI,
you don't just want to toss them on screen whenever they load.
Instead, you request them in advance.
And then when the moment's right, that's when you display.
So their request flow looks like this.
You construct an interstitial ad object, call
a method to load an ad.
Then when you're ready, you check
to make sure it's loaded, and display.
All right.
With that in mind, let's get to some code.
All right.
So here I am in Xcode with my app right there.
It's really simple and I'd just like
to show an interstitial when I hit that button.
And here are the steps to do so.
First, I need to create a GADInterstitial object.
Then I'll request an ad for it, wait for the right moment,
check to make sure it's loaded, and display.
So first I'm going to add an import statement for the Google
Mobile Ads SDK to my view controller code.
There we go.
And I'm going to come down here and add a new property
for my GADInterstitial.
I forgot to make it a pointer.
There we go.
Now I need to initialize it, which I'll do in viewDidLoad.
And this is a long statement, but once I get this typed out,
you'll see that the initializer takes an ad unit ID.
Ad units are created at apps.admob.com.
And you'll want a unique one for each spot in your app
where you show interstitials.
That way you control things like mediation and campaigns
independently.
So there we go.
So now my interstitial's created,
so I need to request an ad.
And the first step is to build a GADRequest object, which
I can do by calling the static request method in GADRequest.
And once I've done that, I'm also
going to give it the simulator's ID
to make sure that I get test ads while I'm testing.
It's very important.
You should always, always use test ads
when you're testing your app.
Perfect.
All right.
Now I just need to call my GADInterstitials load request
method, pass it the request object I just made,
and I'm all set.
Cool.
So the next step is to wait until the right moment
in the flow of the app and then try and display.
We're going to talk a lot more about this step
and how to pick that right moment in our next video.
Today though, since I'm just using a button press,
waiting for the event sort of does that for me.
That means it's on to the last steps.
And I just need to add code to my didReceiveTap method
to check if the interstitial has loaded, and if so, display it.
So you can check to see if your ad has loaded with the IsReady
method.
So I'll use that for my conditional here.
And then to display the ad, I just call
presentFromRoot ViewController,, which in this case
is the current class, so I can use self.
Cool.
Let's make sure everything builds just fine.
There we go.
And hit the button.
And there's my test ad.
Perfect.
All right.
So we created a GADInterstitial, requested an ad,
waited for the right time, checked, and displayed.
Job done.
So that's a basic example of how to get an interstitial working
in iOS.
Now let's take a look at interstitials in Android.
As you'll see, it's the same steps in a different language.
All right.
So here I am in Android Studio.
I've got my app.
It's got a single button.
And I'd like to display an interstitial ad
when somebody taps on it.
And here are the steps to make that happen.
First, I'm going to create an interstitial ad object.
Then I'll request an ad for it, wait until the right moment,
check that it's loaded, and display.
All right.
So in my activity, I'll just drop in a new private member
to hold my interstitial ad object.
There we go.
And then I'll construct it inside my onCreate method here.
Note that interstitials require a context in their constructor,
but since I'm in Activity, I can just use this.
Cool.
And last thing, I just need to give it an ad unit ID.
You can make your own ad units at apps.admob.com.
And it's always a good idea to have
a unique one for each place in your app
where you show an interstitial so that you can configure them
independently.
All right.
So creating object's done and now I need to request an ad.
Just like with banners, to request an ad,
I'll need an ad request object, so I'll make a builder.
There we go.
And I'm going to enter the emulator as a test device
to make sure that I get test ads.
Remember, always, always use test ads
when testing your stuff.
There's the constant.
And the I just called the build method to get my ad request.
All right.
Ad request in hand.
I just need to call the interstitial ads loadAd method,
and that'll kick off the load.
Cool.
All right.
So step three is waiting until the right moment.
And we're going to spend more time on this step
and how to pick that right moment in our next video,
but for this example, I'm just using a button
to keep things simple, so waiting
for the event to fire kind of does that for me.
So that brings me to the last two steps
then, which are checking that the ad is loaded
and displaying.
And I'll handle those right in my onclick handler.
To check if an interstitial has loaded,
you can call the isLoaded method.
So I'll put that in my conditional.
And then to display, I just need to call show.
All right.
Let me run this bad boy and see what happens.
And it's running.
Get a click on here, and there's my test ad.
Perfect.
All right.
So here are the steps once again.
I created an interstitial ad object, requested an ad,
waited for the right moment, checked, and displayed.
Job done.
So now we've got a working ad in both iOS and Android,
but there's a lot more to interstitials
that we haven't had time to cover.
Things like how to load a new interstitial after you've
finished displaying the previous one,
how to pause and resume game loops so you
can avoid wasting CPU cycles, and when's the best time
show an ad?
We're going to spend some time on those in our next episode,
so be on the lookout for it.
In the meantime, I've got some links for you.
You can check out our guides for interstitials on both OSes.
And you can get example code from our GitHub repos,
or by using Android Studio's Import a Sample option.
You might also like AdMob's No Nonsense
Guide to App Monetization.
It's an overview of monetization strategies
that includes code and tips from other engineers.
As always, if you've got a technical question related
to anything you've just seen, stop by our support forum.
And if you've got a question about this video series,
or a tip on something you'd like us to cover,
leave a comment below.
And Gary and I will see you next time.
[MUSIC PLAYING]