在iOS游戏应用程序和Swift中使用AdMob插页式广告

好的,我已经花了好几天的时间来做这件事了。 我已经通过admob的教程,YouTube,堆栈交换,一切,我无法find解决我的问题。

我正试图在我的iOS游戏应用程序中实施admob的插页式广告。 代码编译,构build,运行,游戏播放,但广告不会出现。 这是我的GameViewController.swift

import UIKit import SpriteKit import GoogleMobileAds class GameViewController: UIViewController, GADInterstitialDelegate { //AD STUFF-----------------------------vBELOWv---------------------------------------------- //AD STUFF-----------------------------vBELOWv---------------------------------------------- var interstitialAd: GADInterstitial? func createInterstitialAd() -> GADInterstitial { let request = GADRequest() let interstitial = GADInterstitial(adUnitID: "ca-app-pub-******") request.testDevices = [kGADSimulatorID, "C966DEAC-A2FD-4FBA-80B5-802B7E394C6D", "F3E1897A-3556-4704-9C85-3D70B4672F44","090DCEE5-6B1C-4DFB-83CE-FE800A242008", "1B93E43B-E9B3-4482-8B11-4F3614A26EA2"] interstitial.delegate = self interstitial.loadRequest(request) return interstitial } //func to show the ad func showAd() { if interstitialAd != nil { if interstitialAd!.isReady{ interstitialAd?.presentFromRootViewController(self) } else {print("found not ready")} } } //func to pre-load new ad - calls automatically when closes an ad func interstitialWillDismissScreen(ad: GADInterstitial!) { interstitialAd = createInterstitialAd() } //AD STUFF--------------------------^ABOVE^-------------------------------------------------- //AD STUFF--------------------------^ABOVE^-------------------------------------------------- override func viewDidLoad() { super.viewDidLoad() interstitialAd = createInterstitialAd() if let scene = GameScene(fileNamed:"GameScene") { // Configure the view. let skView = self.view as! SKView skView.showsFPS = true skView.showsNodeCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFill scene.size = self.view.bounds.size skView.presentScene(scene) } } 

这就是我把它叫做我的GameScene

 //makes the restart button appear func createRestartBTN(){ restartBTN = SKSpriteNode(color: SKColor.clearColor(), size: CGSize(width: 200, height: 100)) restartBTN.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)) restartBTN.zPosition = 10 addChild(restartBTN) createReplayText() varForGVC.showAd() } 

所以当游戏结束时,一个函数会使重启button出现。 然后,我希望我的插页式广告popup,以便用户必须退出插页式广告才能点击重播button。 那个variablesvarForGVC是在GameScene.swift中的一个实现,通过varForGVC = GameViewController()

我有我的应用程序与Firebase和admob同步。 我的根目录中有GoogleService-Info.plist。 我的根目录中也有admob所需的框架列表。 我已经安装了Firebase。 我已经尝试把我在我的GameViewController中使用的代码放到我的GameScene.swift中,并且在那里也不工作。 我不知道这是否重要,但我的游戏有一个主屏幕,一个规则屏幕只能通过主屏幕访问,然后用户点击一个播放button,开始在另一个屏幕(GameScene)中玩游戏。 我检查.isReady没有found not ready 。 我需要一些帮助解决这个问题。 现在,我将尝试重新安装框架,看看是否是这样。

这就是我如何使用AdMob的插页式广告 – 使用扩展程序。 我认为这是更干净的解决scheme。 希望它会帮助你!

GameViewController.swift

 class GameViewController: UIViewController { var interstitial: GADInterstitial? override func viewDidLoad() { createInterstitial() } func presentInterstitial() { guard let interstitial = self.interstitial where interstitial.isReady else { return } dispatch_async(dispatch_get_main_queue(), { interstitial.presentFromRootViewController(self) }) } } 

GameViewController + GADInterstitialDelegate.swift

 import GoogleMobileAds extension GameViewController: GADInterstitialDelegate { func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) { print("\(#function): \(error.localizedDescription)") } func interstitialDidDismissScreen(ad: GADInterstitial!) { // Recycle interstitial createInterstitial() unpauseGame() // Some method from GameViewController } func interstitialWillPresentScreen(ad: GADInterstitial!) { pauseGame() // Some method from GameViewController } func createInterstitial() { interstitial = GADInterstitial(adUnitID: interstitialAdUnitID) interstitial!.loadRequest(AdMobHelper.createRequest()) interstitial!.delegate = self } } 

最后一部分 – AdMobHelper.swift

 import GoogleMobileAds let interstitialAdUnitID = "Some Id" class AdMobHelper { static func createRequest() -> GADRequest { let request = GADRequest() request.testDevices = ["Check this in your logs"] return request } } 

我终于明白了。 我使用NSNotifcationCenter。 我在我的viewDidLoad函数的GameViewController.swift中放置了一个观察者,然后是一个我希望popup的接收器。