3

I am following this tutorial

So far I have got it to work when I click the button. However I cannot seem to get it to show an interstitial ad on viewDidLoad.

This is what I have:

override func viewDidLoad() {
  super.viewDidLoad()
  self.interstitialAd = GADInterstitial(adUnitID: "ADUnitID")

  let request = GADRequest()

  // Requests test ads on test devices.
  request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]

  self.interstitialAd.loadRequest(request)
  self.interstitialAd = reloadInterstitialAd()
}

The console keeps showing me this:

2016-02-04 22:27:51.855 GoogleAdMobTutorial[3394:56415] To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ];

When I click the button:

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

The ad shows up. When I dismiss the ad, I get this in the console:

2016-02-04 22:29:16.661 GoogleAdMobTutorial[3394:56743] Request Error: Will not send request because interstitial object has been used.

This is the function that is called when I dismiss the ad:

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

Question

Just for clarity: How do I get an interstitial ad to show when the viewcontroller loads?

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
JamesG
  • 1,552
  • 8
  • 39
  • 86
  • Remove the code from your `interstitialDidDismissScreen` and send a new request, `let request = GADRequest() // Requests test ads on test devices. request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"] self.interstitialAd.loadRequest(request)` – Daniel Storm Feb 05 '16 at 13:18
  • @DanielStorm The ad Code that was there was the default one from Google. No need to remove. – JamesG Feb 05 '16 at 17:20

1 Answers1

8

In any viewController, add this function.

   override func viewDidLoad() {
    super.viewDidLoad()

    let App = UIApplication.sharedApplication().delegate as! AppDelegate
    App.gViewController = self;
    App.showAdmobInterstitial()

// Try below in iOS 14+ and comment above line
// var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: App, selector: Selector("showAdmobInterstitial"), userInfo: nil, repeats: false)
}

// In AppDelegate, declare gViewController as member variable

  var gViewController: UIViewController?
  var mInterstitial: GADInterstitial!

Add these function in AppDelegate class

    func showAdmobInterstitial()
    {
            let kGoogleFullScreenAppUnitID = "ca-app-pub-***";
            self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )
            
            mInterstitial.delegate = self
            let Request  = GADRequest()
            Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
            mInterstitial.loadRequest(Request)
    }
    
    func interstitialDidReceiveAd(ad: GADInterstitial!)
    {
        ad.presentFromRootViewController(self.gViewController)
    }

make sure you added GADInterstitialDelegate in AppDelegate.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {
Guru
  • 21,652
  • 10
  • 63
  • 102
  • 1
    Thank you. P.S is that your own Google Code? Be careful, their policy is that if you get too many clicks on a test device they will ban your account. – JamesG Feb 05 '16 at 07:13
  • @JamesG, Thanks for info, I just removed my Ads Id :) – Guru Feb 05 '16 at 13:15
  • This implementation is not legal with admob policies because, displaying the add takes some time and ViewController view has already appear. @Guru do you have any workarround to make it work? – Angel Sep 26 '20 at 18:00
  • 1
    @Ángel got any log from Admob like "The provided view controller is not being presented" ? solution is wait 1 second in viewDidLoad then call this : [self performSelector:@selector(showAdmobInterstitial) withObject:nil afterDelay:1.0 ]; – Guru Sep 27 '20 at 14:37
  • @Guru no, I don't get any log from admob, but in the intersticial admob recommendations the prohibe this type of implementation. Could you edit your answer and post it with the wait 1 second implementantion? I am a bit noob with Swiift – Angel Sep 27 '20 at 16:22
  • 1
    @Ángel check this https://stackoverflow.com/questions/24170282/swift-performselectorwithobjectafterdelay-is-unavailable – Guru Sep 28 '20 at 12:09