5

Is this code correct to implement an interstitial ad?

enter image description here

Because when I launch it I get this,

<Google> Cannot present interstitial.  It is not ready.
Amar
  • 13,202
  • 7
  • 53
  • 71

5 Answers5

23

You need to wait until the ad loads successfully and only then present it in case of Interstitial ads. Otherwise the ads wont show up.

To do this conform to the GADInterstitialDelegate protocol and set your view controller as the delegate of interstitial_.

interstitial_.delegate = self;

Then implement interstitialDidReceiveAd as follows.

- (void)interstitialDidReceiveAd:(GADInterstitial *)ad
{
    [interstitial_ presentFromRootViewController:self];
}

For more reference, please check Interstitials ads

bobobobo
  • 64,917
  • 62
  • 258
  • 363
Rahul Patel
  • 5,858
  • 6
  • 46
  • 72
  • So should I just copy all of this and implement somewhere? Sorry, I'm new to all this. @Rahul Patel http://i.gyazo.com/ba303f5cfb14042559cf59a93bb491f3.png –  Apr 16 '14 at 17:07
  • Yes you need to implement in your viewController. This method would be fire automatically when something happens regarding ads – Rahul Patel Apr 17 '14 at 04:33
  • 1
    Note, proper method declaration is `- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial;` – Iłya Bursov Jun 15 '14 at 23:39
  • I've tried a few different things.. but I can't get the interstitialWillDismissScreen implementation in Swift. Can anyone translate this solution to Swift? – cloudberry Nov 03 '14 at 14:58
  • Should you not change the declaration of your Objective-C class to indicate you conform to the `` protocol? (ie write `@interface ViewController : UIViewController ` for the viewcontroller that will be the delegate?) – bobobobo Dec 28 '20 at 20:20
  • @bobobobo : Yes. It is explained as simple text in answer. – Rahul Patel Jan 07 '21 at 06:26
4

Swift 3.0

in viewDidLoad() or createAndLoadInterstitial()

interstitial.delegate = self

then implement

func interstitialDidReceiveAd(_ ad: GADInterstitial) {
    interstitial.present(fromRootViewController: self)
}
Emma Labbé
  • 667
  • 7
  • 18
1

If you want load google interstitial ads again on next page or current page use below code..

//On view Did load add following line on your next page or current page view controller.m

self.interstitial = [self createAndLoadInterstitial];

//create following methods on your next page or current page view controller.m

- (GADInterstitial *)createAndLoadInterstitial {
    GADInterstitial *interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"];
    interstitial.delegate = self;
    [interstitial loadRequest:[GADRequest request]];
    return interstitial;
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad
{
    [self.interstitial presentFromRootViewController:self];
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial 
{
//leave this method as empty    
}

now you will able to receive ads will shown

abdul sathar
  • 2,395
  • 2
  • 28
  • 38
1

I don't think you have to use interstitialDidReceiveAd method. This is not recommended by admob documentation. Maybe you can use DispatchQueue.main.async in viewDidLoad()

if (mInterstitialAd.isReady)
{
    DispatchQueue.main.async
    {
        mInterstitialAd.present(fromRootViewController: self)
    }
}
Numan Karaaslan
  • 1,365
  • 1
  • 17
  • 25
0

Swift 5.0

in viewDidLoad()

Make sure to set the delegate for your View Controller by doing so

interstitial.delegate = self

then in the delegate methods you could implement the following

func interstitialDidReceiveAd(_ ad: GADInterstitial) {
    interstitial.present(fromRootViewController: self)
}