AdMob的IOS与Swift在SpriteKit

AdMob的IOS与Swift在SpriteKit

本文介绍了AdMob的IOS与Swift在SpriteKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照文件,我发现(googleAd在SWIFT)。当我在 SpriteKit 现场进行。命令行 bannerView?.rootViewController =自出现错误。

gamescene无法转换为的UIViewController

  bannerView = GADBannerView(adSize:kGADAdSizeBanner)
            bannerView?.adUnitID =xxxxxxxxxxxxxxxxxxxxx
            bannerView?.delegate =自
            ?bannerView .rootViewController =自//  - >错误gamescene无法转换为的UIViewController
            self.view?.addSubview(bannerView!)
            bannerView?.loadRequest(GADRequest())

            计时器?.invalidate()
            定时器= NSTimer.scheduledTimerWithTimeInterval(40,目标:自我,选择:GoogleAdRequestTimer,用户信息:无,重复:真)
 

解决方案

您需要把这个code在viewDidLoad中内GameViewController.swift不GameScene.swift。

记住GADBannerViewDelegate添加到类。

例如:

 类GameViewController:UIViewController的,GADBannerViewDelegate {

VAR一幕:GameScene!
VAR adBannerView:GADBannerView!

覆盖FUNC viewDidLoad中(){
    super.viewDidLoad()

        //配置视图。
        让skView =查看SKView
        skView.showsFPS = FALSE
        skView.showsNodeCount = FALSE
        skView.showsPhysics = FALSE

        / *雪碧套件适用额外的优化来提高渲染性能* /
        skView.ignoresSiblingOrder =真

        / *设置缩放模式缩放以适应窗口* /
        场景= GameScene(尺寸:skView.bounds.size)
        scene.scaleMode = .AspectFill

        skView。presentScene(场景)

    adBannerView = GADBannerView(框架:CGRectMake(0,0,self.view.frame.size.width,50))
    adBannerView.delegate =自
    adBannerView.rootViewController =自
    adBannerView.adUnitID =广告ID

    VAR reqAd = GADRequest()
    reqAd.testDevices = [GAD_SIMULATOR_ID] //如果你想测试广告的
    adBannerView.loadRequest(reqAd)
    self.view.addSubview(adBannerView)


}
 

我已经提到两个环节,可提供有关如何将谷歌广告的广告(AdMob的)到一个应用程序的例子。

谷歌的广告同斯威夫特例1(更详细)

谷歌的广告同斯威夫特例2

我希望这有助于

I have followed the Documentation that I found (googleAd in swift). when I performed in my SpriteKit scene. Command line "bannerView? .rootViewController = Self" appear error.

gamescene not convertible to UIViewController

 bannerView = GADBannerView(adSize: kGADAdSizeBanner)
            bannerView?.adUnitID = "xxxxxxxxxxxxxxxxxxxxx"
            bannerView?.delegate = self
            bannerView?.rootViewController = self // -> Error "gamescene not convertible to UIViewController"
            self.view?.addSubview(bannerView!)
            bannerView?.loadRequest(GADRequest())

            timer?.invalidate()
            timer = NSTimer.scheduledTimerWithTimeInterval(40, target: self, selector: "GoogleAdRequestTimer", userInfo: nil, repeats: true)
解决方案

You need to put this code in viewDidLoad within GameViewController.swift not in GameScene.swift.

Remember to add GADBannerViewDelegate to the class.

Example:

class GameViewController: UIViewController, GADBannerViewDelegate {

var scene: GameScene!
var adBannerView: GADBannerView!

override func viewDidLoad() {
    super.viewDidLoad()

        // Configure the view.
        let skView = view as SKView
        skView.showsFPS = false
        skView.showsNodeCount = false
        skView.showsPhysics = false

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene = GameScene(size: skView.bounds.size)
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

    adBannerView = GADBannerView(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
    adBannerView.delegate = self
    adBannerView.rootViewController = self
    adBannerView.adUnitID = "YOUR AD ID"

    var reqAd = GADRequest()
    reqAd.testDevices = [GAD_SIMULATOR_ID] // If you want test ad's
    adBannerView.loadRequest(reqAd)
    self.view.addSubview(adBannerView)


}

I have referenced two links that provide examples on how to include Google Ad's (AdMob) into an app.

Google Ad's with Swift Example 1 (more detailed)

Google Ad's with Swift Example 2

I hope this helps

这篇关于AdMob的IOS与Swift在SpriteKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:39