【问题标题】:AdMob Interstitial Ad in SpriteKit GameSpriteKit 游戏中的 AdMob 插页式广告
【发布时间】:2017-04-19 23:43:02
【问题描述】:

每次我的游戏转换到 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”以匹配发送的通知。

【问题讨论】:

  • 请分享你的一些代码。

标签: ios sprite-kit admob


【解决方案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

【讨论】:

  • 我明白您为什么这样做,但广告仍然没有显示。如果我不从那里加载,我的 didRecieveAd 函数中应该包含什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
相关资源
最近更新 更多