Question

I am trying to implement Admob Interstitial ads on ios.

Here is what I have so far, this is my first time ever touching objective-c so please be kind.

// Simple Admob Interstitial support for Monkey - IOS

// admobInterstitial.ios.h

#import "GADInterstitial.h"


class AdmobInterstitial {

    // the kind of "singleton"
    static AdmobInterstitial *_admob;
    // the ad
    GADInterstitial *_interstitialAd;
    // ad Unit ID
    NSString *adUnitId;

public:
    AdmobInterstitial();

    // creates an instance of the object and start the thread
    static AdmobInterstitial *GetAdmobInterstitial(String adUnitId);

    // displays the ad to the user if it is ready
    void ShowAd();
};


// admobInterstitial.ios.cpp

AdmobInterstitial *AdmobInterstitial::_admob;

AdmobInterstitial::AdmobInterstitial():_interstitialAd(0) {

}

AdmobInterstitial *AdmobInterstitial::GetAdmobInterstitial(String adUnitId) {
    if( !_admob ) _admob=new AdmobInterstitial();
    _admob->adUnitId = adUnitId.ToNSString();
    return _admob;
}

void AdmobInterstitial::ShowAd() {
    // create ad (should this go here or earlier?)
    _interstitialAd = [[GADInterstitial alloc] init];

    if (_interstitialAd) {
        _interstitialAd.adUnitID = adUnitId;
        [_interstitialAd loadRequest:[GADRequest request]];

        if (_interstitialAd.isReady) {
            BBMonkeyAppDelegate *appDelegate=(BBMonkeyAppDelegate*)[[UIApplication sharedApplication] delegate];
            UIViewController *rootViewController = appDelegate->viewController;
            [_interstitialAd presentFromRootViewController:rootViewController];
        }
    }
}

I am calling ShowAd() in my game after a player dies and clicks the restart button. Currently, _interstitialAd.isReady is not coming back as true.

This is the documentation I used to get started https://developers.google.com/mobile-ads-sdk/docs/admob/advanced#ios

It says that "You may invoke loadRequest: at any time, however, you must wait for GADInterstitialDelegate's interstitialDidReceiveAd: to be called before displaying the creative."

I assume this is the problem I am running into. I think I am calling loadRequest before interstitialDidReceiveAd. However, the document doesn't show an example of how I would wait for this method to be called.

Can anyone help with this?

EDIT: Now works and shows the Ad the 1st time I call ShowAd(), however doesn't show the ad any time after the 1st time this function is called

// Simple Admob Interstitial support for Monkey - IOS

// admobInterstitial.ios.h

#import "GADInterstitial.h"


class AdmobInterstitial {

    // the kind of "singleton"
    static AdmobInterstitial *_admob;
    // the ad
    GADInterstitial *_interstitialAd;
    // ad Unit ID
    NSString *adUnitId;

    void loadAd();

public:
    AdmobInterstitial();

    // creates an instance of the object and start the thread
    static AdmobInterstitial *GetAdmobInterstitial(String adUnitId);

    // displays the ad to the user if it is ready
    void ShowAd();
};


// admobInterstitial.ios.cpp

AdmobInterstitial *AdmobInterstitial::_admob;

AdmobInterstitial::AdmobInterstitial():_interstitialAd(0) {

}

AdmobInterstitial *AdmobInterstitial::GetAdmobInterstitial(String adUnitId) {
    if( !_admob ) _admob=new AdmobInterstitial();
    _admob->adUnitId = adUnitId.ToNSString();
    _admob->loadAd();
    return _admob;
}

void AdmobInterstitial::loadAd() {

    // testing
    _interstitialAd = [[GADInterstitial alloc] init];

    if (_interstitialAd) {
        _interstitialAd.adUnitID = adUnitId;
        [_interstitialAd loadRequest:[GADRequest request]];
    }
    // end testing

}

void AdmobInterstitial::ShowAd() {
    // create ad (should this go here or earlier?)
    //_interstitialAd = [[GADInterstitial alloc] init];

    if (_interstitialAd) {
        //_interstitialAd.adUnitID = adUnitId;
        //[_interstitialAd loadRequest:[GADRequest request]];

        if (_interstitialAd.isReady) {
            BBMonkeyAppDelegate *appDelegate=(BBMonkeyAppDelegate*)[[UIApplication sharedApplication] delegate];
            UIViewController *rootViewController = appDelegate->viewController;
            [_interstitialAd presentFromRootViewController:rootViewController];
        }
    }
}
Was it helpful?

Solution

OK. Firstly isAdReady==false will occur when there is no ad available to show. Ie it is a natural condition. You can mitigate it by using mediation.

But in your case it is likely that no ad was available to show because there had no been enough time to send the ad request and receive a response before asking that question.

What you need to do is to call loadAd early. Ie when you game first starts. Then at a natural break point in your game (after the player dies) check is isAdReady is true, and if so show the ad.

When you show the ad or when the game starts up again, call loadAd again so that you will have an ad ready again the next time you want to show it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top