How to make an AdMob interstitial (either on a timer or #of button presses) in Objective-C?

294 views
Skip to first unread message

Adem Redzic

unread,
Mar 19, 2016, 4:15:35 AM3/19/16
to Google Mobile Ads SDK Developers
Hello AdMob Forum, I want to implement interstitials into my Objective-C project but the official documentation only got me so far. I was wondering if there was a way to set them up from scratch on a timer? Or have it so 1 in every 5 button clicks will show an interstitial? Thanks in advance.

Vu Chau (Mobile Ads SDK Team)

unread,
Mar 19, 2016, 11:40:42 AM3/19/16
to Google Mobile Ads SDK Developers
Hi Adem,

What you'd do is having everything else already set up on your View Controller (that is, instantiating your GADInterstitial and your GADRequest, and send out an ad request), and then on a timer or certain touch action (you will decide how to implement this logic), check if the interstitial is ready.  If it is ready, present it.  

Once the user closes out the ad, you will request and preload a new interstitial in interstitialDidDismissScreen.  Keep in mind that GADInterstitial is a one-time object, so you'd need to allocate a new one to request a new interstitial ad.

Note that we encourage that developers follow the default workflow.  You wouldn't want to do everything from scratch dynamically at once.  That would degrade the performance of your app, and cause a negative user experience.

Vu Chau
Mobile Ads SDK Team

Adem Redzic

unread,
Mar 19, 2016, 1:06:55 PM3/19/16
to Google Mobile Ads SDK Developers
Hey Vu,

When I said from scratch I meant I needed help getting a walkthrough from the beginning. I don't know where to start and would like some assistance as to which code goes into which viewcontroller and so on and so forth.

Any help would be apprecciated.

-Adem Redzic

Vu Chau (Mobile Ads SDK Team)

unread,
Mar 19, 2016, 7:47:38 PM3/19/16
to Google Mobile Ads SDK Developers
Hi Adem,

Have you checked out our sample project we host on GitHub?  If not, download it and launch the interstitial sample in Xcode, and I'm going to walk you through it:
  • In your viewDidLoad, call [self createAndLoadInterstitial].  That will get your interstitial object, initialize it with your ad unit ID, and make an ad request, like the following:

- (void)createAndLoadInterstitial {
   self.interstitial =
   [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"];
   self.interstitial.delegate = self;

   GADRequest *request = [GADRequest request];
   [self.interstitial loadRequest:request];
}

  • Wire a button touch action to (IBAction)playAgain:(id)sender method, which handles the logic to check if an interstitial is ready and present it.  Here I have a number counter that when it's divisible by 5, the code will gets called:
int counter = 0;
#pragma Interstitial button actions
- (IBAction)playAgain:(id)sender {
   if (counter % 5 == 0) {
       if (self.interstitial.isReady) {
           [self.interstitial presentFromRootViewController:self];
       } else {
           [[[UIAlertView alloc] initWithTitle:@"Interstitial not ready"
                                 message:@"The interstitial didn't finish loading or failed to load"
                                 delegate:self
                                 cancelButtonTitle:@"Drat"
                                 otherButtonTitles:nil] show];
       }
   }
   counter++;
}
  • Leave all other code (the delegates, the animations, etc.) in place.  Clearly I have simplified the logic and eliminated all the other timer options that come out of the box.  Once you've reached this stage, your interstitial will display once every five clicks.
Let us know if you have additional questions,

Vu Chau
Mobile Ads SDK Team

Adem Redzic

unread,
Mar 20, 2016, 12:30:49 AM3/20/16
to Google Mobile Ads SDK Developers
Hello Vu,

I've tried to sample project and the code provided many time with no avail. I'm still getting a ton of errors. First in my viewcontroller.h file I added the IBAction code and then pasted the viewdidload code above. When I tried to link to a sender in my viewcontroller.m using the second block of code provided I still get errors. I even tried copy and pasting all the code from the sample project into mine and still get errors. I have the GoogleMobileAds SDK imported and receive banner ads just fine.

I'm pretty stuck on what to do, sorry for being a noob.

Any help would be appreciated,

-Adem

Vu Chau (Mobile Ads SDK Team)

unread,
Mar 20, 2016, 10:10:30 AM3/20/16
to Google Mobile Ads SDK Developers
Hi Adem,

What are the errors you are seeing?  And what are the steps to reproduce them? 
Feel free to throw in a couple of screenshots if it helps.  Here's the modified code.

Vu Chau
Mobile Ads SDK Team

P/S: The viewDidLoad should go in your ViewController.m, if it didn't.

Adem Redzic

unread,
Mar 22, 2016, 3:45:04 PM3/22/16
to Google Mobile Ads SDK Developers
Hey Vu,

Sorry I haven't been ignoring you. Just wanted to let you know it will be a few days before I can get back to my program and then I will follow the code you sent me.

-Adem

Adem Redzic

unread,
Mar 26, 2016, 11:56:48 PM3/26/16
to Google Mobile Ads SDK Developers
Hello Vu,

The code worked great! It was a little difficult to merge my view controllers but it turned out alright. It functions just as intended. Thank you so much for your help.

-Adem

Vu Chau (Mobile Ads SDK Team)

unread,
Mar 27, 2016, 5:09:11 PM3/27/16
to Google Mobile Ads SDK Developers
No problem! Let us know if you need anything else.

Vu Chau
Mobile Ads SDK Team

Reply all
Reply to author
Forward
0 new messages