GADInterstitial在视图层次结构中重复

时间:2017-04-11 15:39:46

标签: ios firebase admob

我有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工作并且看起来很好但是当我调试时我发现了一些奇怪的东西:

enter image description here

同一视图的四个实例。有谁知道它好吗?或许我在AdsManager犯了错误?也许Firebase实施错误?

1 个答案:

答案 0 :(得分:2)

GADUIKitWebView的数量取决于所呈现的广告类型。视频,图片和文字广告都将采用不同的布局。根据使用的内存量,我认为它是正确的。

查看交互式GADInterstitial广告的层次结构:

enter image description here

除此之外,您的实施确实存在问题。你的问题在于这个功能:

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()
}