首页 文章

SpriteKit游戏中的AdMob插页式广告

提问于
浏览
1

每次我的游戏过渡到GameOver场景时,我都会尝试展示AdMob插页式广告 . 但是,只有在我的视图控制器中将其初始化函数放入viewDidLoad()函数中时,才会显示广告 . 我在游戏上设置了一个通知中心,并且在进入GameOver场景时尝试发送通知,以触发初始化广告的功能,但这并不能解决问题 . 我想知道如何在任何给定时间从场景触发它而不是在启动应用程序时立即显示它,这就是将它放在视图控制器的viewDidLoad函数中 .

在我的GameViewController中有以下两个功能:

public func initAdMobInterstitial() {

    adMobInterstitial = GADInterstitial(adUnitID: AD_MOB_INTERSTITIAL_UNIT_ID)
    adMobInterstitial.delegate = self
    let request = GADRequest()
    request.testDevices = ["ddee708242e437178e994671490c1833"]

    adMobInterstitial.load(request)

}

func interstitialDidReceiveAd(_ ad: GADInterstitial) {

    ad.present(fromRootViewController: self)

}

在这里,我已经注释掉了initAdMobInterstitial,但是当它被取消注释时,广告会弹出并正常工作 . 应用程序第一次启动时会立即显示此弹出窗口 .

override func viewDidLoad() {
    super.viewDidLoad()

    //initAdMobInterstitial()

    initAdMobBanner()

    NotificationCenter.default.addObserver(self, selector: #selector(self.handle(notification:)), name: NSNotification.Name(rawValue: socialNotificationName), object: nil)

    let scene = Scene_MainMenu(size: CGSize(width: 1024, height: 768))
    let skView = self.view as! SKView

    skView.isMultipleTouchEnabled = true

    skView.ignoresSiblingOrder = true

    scene.scaleMode = .aspectFill

    _ = SGResolution(screenSize: view.bounds.size, canvasSize: scene.size)

    skView.presentScene(scene)

}

现在,在我的一个名为GameOver的场景中,我想要弹出广告 . 每当场景出现时我都希望它出现,所以每次玩家输掉并击中游戏时 . 使用通知中心,您可以在我的视图控制器类中看到,我已尝试发送通知并将其处理...

override func didMove(to view: SKView) {

    self.sendNotification(named: "interNotif")

}

...通过此函数,也可以在视图控制器类中找到

func handle(notification: Notification) {

    if (notification.name == NSNotification.Name(rawValue: interstitialNotificationName)) {

        initAdMobInterstitial()

    }
}

另外作为注释,在我的视图控制器中,我已声明interstitialNotificationName等于字符串“interNotif”以匹配发送的通知 .

1 回答

  • 1

    加载后不要出现 GADInterstitial . 您的通知功能应该呈现它 . 然后,一旦用户驳回了另一个广告请求 . 例如:

    override func viewDidLoad() {
        super.viewDidLoad()
        // Load the ad 
        initAdMobInterstitial()
    }
    
    func interstitialDidReceiveAd(_ ad: GADInterstitial) {
        // Do not present here
        // ad.present(fromRootViewController: self)
    }
    
    func handle(notification: Notification) {
        if (notification.name == NSNotification.Name(rawValue: interstitialNotificationName)) {
            // Check if the GADInterstitial is loaded
            if adMobInterstitial.isReady {
                // Loaded so present it
                adMobInterstitial.present(fromRootViewController: self)
            }
        }
    }
    
    // Called just after dismissing an interstitial and it has animated off the screen.
    func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        // Request new GADInterstitial here
        initAdMobInterstitial()
    }
    

    有关 GADInterstitialDelegate 广告事件的完整列表,请参阅AdMob iOS Ad Events .

相关问题