AdMob横幅广告未显示Swift 3

时间:2016-12-05 19:57:04

标签: ios firebase sprite-kit admob swift3

我正在开发一款SpriteKit游戏,并正在通过AdMob(Google Firebase)实施横幅广告。我已经按照Google的教程herehere设置了您的第一个广告请求,但是当我在SIM卡或我的设备(iPhone 6s)上运行应用时,横幅广告没有显示。根据教程,我通过Storyboard实现了横幅视图,其余部分以编程方式设置。下面是我的视图控制器的代码。

问:为什么我的横幅不显示?

后续问:是否可以仅在某些SKScenes上显示横幅?

import UIKit
import SpriteKit
import GoogleMobileAds

class GameViewController: UIViewController, GADBannerViewDelegate {

    @IBOutlet weak var bannerView: GADBannerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
        bannerView.delegate = self
        bannerView.adUnitID = "ca-app-pub-9474695450721030/1823667708"
        bannerView.rootViewController = self
        let req = GADRequest()
        req.testDevices = ["91fbd46dff1179ce0a5e7226cea1ee0b", kGADSimulatorID]
        req.tag(forChildDirectedTreatment: true)
        bannerView.load(GADRequest())
        view.addSubview(bannerView)
        showBanner()
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if let skView = self.view as? SKView {
            if skView.scene == nil {

                let aspectRatio = view.bounds.size.height / view.bounds.size.width
                let scene = MenuScene(size: CGSize(width: 750, height: 750 * aspectRatio))

                scene.scaleMode = .aspectFill
                skView.ignoresSiblingOrder = true

                if kDebug {
                    skView.showsFPS = true
                    skView.showsDrawCount = true
                    skView.showsNodeCount = true
                    skView.showsPhysics = true
                }

                let transition = SKTransition.fade(with: SKColor.black, duration: 0.5)

                skView.presentScene(scene, transition: transition)
            }
        }
    }

    func showBanner() {
        bannerView.isHidden = false
        let request = GADRequest()
        request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
        bannerView.load(request)
    }

    func hideBanner() {
        bannerView.isHidden = true
    }
}

1 个答案:

答案 0 :(得分:3)

首先,每次运行showAd()时,您都不需要申请新的横幅广告。简单地取消隐藏它。您在启动时加载的广告将在整个生命周期内保留,并自动刷新。

其次,在您的viewDidLoad代码中,您正在初始化横幅广告,然后加载一个全新的横幅广告。确保您正在加载您正在设置的相同请求。

至于从SKScene调用GameViewController中的函数,请看一下:Call GameViewController function from SKScene