Admob Interstitial isReady是假的

时间:2014-03-28 21:57:59

标签: ios objective-c asynchronous admob interstitial

我正在尝试在ios上实施Admob非页内广告。

这是我到目前为止所做的,这是我第一次触及Objective-c所以请善待。

// 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];
        }
    }
}

在玩家死亡并点击重启按钮后,我在游戏中调用ShowAd()。 目前,_interstitialAd.isReady没有回归真实。

这是我以前开始使用的文档 https://developers.google.com/mobile-ads-sdk/docs/admob/advanced#ios

它说"您可以调用loadRequest:但是,您必须等待GADInterstitialDelegate的interstitialDidReceiveAd:在显示广告素材之前被调用。"

我认为这是我遇到的问题。我想我在interstitialDidReceiveAd之前调用loadRequest。但是,该文档没有显示我将如何等待调用此方法的示例。

任何人都可以帮忙吗?

编辑:现在工作并在我第一次调用ShowAd()时显示广告,但是在第一次调用此函数后的任何时间都没有显示广告

// 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];
        }
    }
}

1 个答案:

答案 0 :(得分:2)

行。首先,当没有可用广告显示时,将发生isAdReady == false。即这是一个自然条件。您可以使用中介缓解它。

但在您的情况下,可能没有广告可供展示,因为在提出问题之前没有足够的时间发送广告请求并收到回复。

您需要做的是尽早调用loadAd。即,当你的游戏第一次开始。然后在游戏的自然断点处(玩家死后),检查isAdReady是否为真,如果是,则显示广告。

当您展示广告或游戏再次启动时,请再次致电loadAd,以便下次要展示广告时再次投放广告。