我有一个GADInterstitial
的实现
class AdsManager {
let adsAppId = MY_ID
let fullScreenAdUnitId = MY_ID
static let manager: AdsManager = AdsManager()
private var interstitial: GADInterstitial!
private init () {
createAndLoadInterstitial()
}
func presentInterstitial(fromViewController viewController: UIViewController) {
if interstitial.isReady {
interstitial.present(fromRootViewController: viewController)
}
else {
print("Interstitial is not ready")
}
createAndLoadInterstitial()
}
private func createAndLoadInterstitial() {
//TODO: replace test ad unit id with production
interstitial = GADInterstitial(adUnitID: MY_ID)
interstitial.load(GADRequest())
}
}
然后我像这样显示此广告:
func touchedButton(_ sender: UIButton) {
AdsManager.manager.presentInterstitial(fromViewController: self)
}
Everythink可以正常工作并且看起来不错,但是当我调试时,发现了一些奇怪的地方:
同一视图的四个实例。有人知道这很好吗?还是我的
AdsManager
错误?也许Firebase实施错误? 最佳答案
GADUIKitWebView
的数量取决于所展示广告的类型。视频,图像和文字广告将具有不同的布局。基于正在使用的内存量,我认为它是正确的。
查看交互式GADInterstitial
广告的层次结构:
除此之外,您的实现确实存在问题。您的问题在于此功能:
func presentInterstitial(fromViewController viewController: UIViewController) {
if interstitial.isReady {
interstitial.present(fromRootViewController: viewController)
}
else {
print("Interstitial is not ready")
}
createAndLoadInterstitial() // PROBLEM
}
您应该将
createAndLoadInterstitial()
移到else
中,以便仅在实际需要时才加载另一个GADInterstitial
。例如:func presentInterstitial(fromViewController viewController: UIViewController) {
if interstitial.isReady {
interstitial.present(fromRootViewController: viewController)
}
else {
print("Interstitial is not ready")
createAndLoadInterstitial()
}
}
除此之外,您应该实现
GADInterstitial
delegate methods并仅在关闭屏幕后才加载另一个GADInterstitial
。例如:/// Called just after dismissing an interstitial and it has animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
print("interstitialDidDismissScreen")
createAndLoadInterstitial()
}