【问题标题】:Using AdMob interstitial ads in iOS game applications and Swift在 iOS 游戏应用和 Swift 中使用 AdMob 插页式广告
【发布时间】:2016-06-20 03:02:38
【问题描述】:

好的,我已经花了几天时间来做这件事。我已经浏览了 admob 的教程、youtube、堆栈交换等所有内容,但我找不到解决问题的方法。

我正在尝试在我的 iOS 游戏应用程序中实现 admob 的插页式广告。代码编译、构建、运行、游戏开始,但广告不会出现。这是我的 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()
}

所以当游戏结束时,一个函数会出现一个重启按钮。然后我希望我的插页式广告弹出,因此用户必须退出插页式广告才能点击重播按钮。该变量varForGVCvarForGVC = GameViewController() 较早在GameScene.swift 中实现的

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

【问题讨论】:

    标签: ios swift sprite-kit admob interstitial


    【解决方案1】:

    这就是我使用 AdMob 的插页式广告的方式 - 使用扩展程序。我认为这是更干净的解决方案。希望对你有帮助!

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

    【讨论】:

    • 谢谢你,这确实清理了我的代码不少! NSNotificationCenter 有效,但这更干净。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 2015-07-05
      相关资源
      最近更新 更多