即使阅读了我能找到的有关该主题的所有StackOverflow帖子,我也无法显示GoogleAdMob插页式广告。通过调试,我可以知道广告在启动应用后成功加载(我检查了调用interstitialDidReceiveAd
时,interstitial.isReady
等于true
),但是当我调用函数以显示广告,突然变成false
,并且广告还没准备好展示。
我密切关注了Google指导视频,当该指导视频无效时,我也尝试了一些Youtube视频中的方法。我试图将插页式对象和显示功能放置到viewController,gameScene和adMobDelegate文件中,但无济于事。
-adMobDelegate类中的声明
var interstitial: GADInterstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
-从视图确实加载到viewController中
var admobDelegate = AdMobDelegate()
admobDelegate.getInterstitialAd()
-哪个呼叫(在adMobDelegate中)
func getInterstitialAd(){
self.interstitial.delegate = self
let interstitialRequest = GADRequest()
interstitialRequest.testDevices = [ kGADSimulatorID ]
interstitial.load(interstitialRequest)
}
我已经确认,这成功地加载了广告(我什至可以从interstitialDidReceiveAd(...)
展示广告,但是当调用showAd()
(如下定义)时,广告突然变得无法显示:
-完成关卡后,在gameScene中调用
func winAnnounce() {
// other code to tell the user they won
let adDelegate = AdMobDelegate()
adDelegate.showAd()
}
-adAdbDelegate中的showAd()
func showAd() {
print("Ad Loaded:",interstitial.isReady)
if interstitial.isReady {
// this currently won't display the ad, as it is not in the view
// hierarchy (which I will solve later), but we are not even getting
// to this point because interstitial.isReady = false ALL OF THE SUDDEN
interstitial.present(fromRootViewController: GameViewController())
} else {
// createAd() basically does the same thing as getInterstitialAd
interstitial = createAd()
print("Ad wasn't ready")
}
}
我们每次都会看到“广告尚未准备好”的信息。这导致我们执行else语句,该语句为interstitial
创建了一个新广告,该广告成功加载并再次isReady = true
!任何帮助或想法都将不胜感激!