我正在尝试在 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];
}
}
}