ios - interstitialWillDismissScreen 只被调用一次

标签 ios swift admob interstitial

我在我的 swift 项目中使用 Admob 插页式广告,但我在重新加载广告时遇到了问题。第一个插页式广告显示正常,当调用 interstitialWillDismissScreen 时,它退出回到游戏并按预期重新加载新的插页式广告。但是,interstitialDidReceiveAd 和 interstitialWillDismissScreen 都没有被再次调用,因此在显示第二个广告后,没有其他广告通过。我错过了什么?

import GoogleMobileAds

let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)

class GameScene: SKScene, GKGameCenterControllerDelegate, GADInterstitialDelegate {

    var interstitial: GADInterstitial!

    override func didMoveToView(view: SKView) {

        //preload interstitial ad
        if NSUserDefaults.standardUserDefaults().boolForKey("paidToRemoveAds") == false {
            interstitial = loadAd()
        }

        interstitial.delegate = self
    }

    func gameOver() {
        runGoogleAd()
    }

    func loadAd() -> GADInterstitial {
        let ad = GADInterstitial(adUnitID: "ca-app-pub-2963481692578498/7292484569")
        let request = GADRequest()
        request.testDevices = [kGADSimulatorID]
        ad.loadRequest(request)
        return ad
    }

    func runGoogleAd() {
        if interstitial.isReady {
            interstitial.presentFromRootViewController((appDelegate.window?.rootViewController)!)
        }
    }

    func interstitialDidReceiveAd(ad: GADInterstitial!) {
        print("ad loaded")
    }

    func interstitialWillDismissScreen(ad: GADInterstitial!) {
        interstitial = loadAd()
        print("loading new ad")
    }
}

最佳答案

我假设 loadAd() 创建了一个新实例?您需要在新实例上设置委托(delegate):

func interstitialWillDismissScreen(ad: GADInterstitial!) {
    interstitial = loadAd()

    interstitial.delegate = self  // <-- You forgot this.

    print("loading new ad")
}

实际上,您应该在 loadAd() 中设置委托(delegate),因为它是此类的实例成员。

关于ios - interstitialWillDismissScreen 只被调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38318853/

相关文章:

ios - 无法将我的 iOS 应用程序扩展到 iPhone 5 和 4

objective-c - 如何在 AVPlayer 中播放 Youtube 视频?

swift - 在 Swift 3 上使用 NSFileHandle

ios - 如何将 GIf 图像添加为 View 图层?

swift - 快速代码中的自动布局问题

android - 任何适用于 Android Phonegap 的 admob 教程

android - 如何从一个类调用方法或部分代码到另一个类?

android - API 级别 28 的编译错误

ios - 手势不适用于包含 UITableViewControllers 的 UIPageViewController

ios - swift 中的错误顺序 tableview.reloaddata()