ios - Admob Interstitial isReady 为 False

标签 ios objective-c asynchronous admob interstitial

我正在尝试在 ios 上实现 Admob Interstitial 广告。

这是我目前的情况,这是我第一次接触 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 不会恢复为 true。

这是我用来入门的文档 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];
        }
    }
}

最佳答案

好的。首先 isAdReady==false 会在没有广告可展示时发生。即这是自然条件。您可以使用调解来缓解它。

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

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

当您显示广告或游戏再次启动时,再次调用 loadAd 以便您下次要显示广告时再次准备好广告。

关于ios - Admob Interstitial isReady 为 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22723894/

相关文章:

ios - 如何将构建发送到已配置的远程人员的 iPhone

ios - iPhone 6 Plus 上的 UIImage 缩放

iphone - UIImageView 适合宽度

iOS:当应用程序未运行时检测蓝牙设备何时断开连接

objective-c - 在添加到数组之前格式化为小数点后2位

python - Cassandra 或 MongoDB 可实现良好的扩展性和大量查询

perl - 如何将管道设置为 O_NONBLOCK perl

ios - UIImageView 意外改变大小

ios - 你如何在 iOS 7 中获得 ADBannerView 的渲染宽度?

javascript - 关于通过 let 声明变量的变量范围的疑问