我有一个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可以正常工作并且看起来不错,但是当我调试时,发现了一些奇怪的地方:

ios -  View 层次结构中的GADInterstitial重复项-LMLPHP

同一视图的四个实例。有人知道这很好吗?还是我的AdsManager错误?也许Firebase实施错误?

最佳答案

GADUIKitWebView的数量取决于所展示广告的类型。视频,图像和文字广告将具有不同的布局。基于正在使用的内存量,我认为它是正确的。

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

ios -  View 层次结构中的GADInterstitial重复项-LMLPHP

除此之外,您的实现确实存在问题。您的问题在于此功能:

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

07-27 20:43