如何使用swift,spritekit和xcode调用admob插页式广告? [英] How to call admob interstitial ad using swift, spritekit and xcode?

查看:94
本文介绍了如何使用swift,spritekit和xcode调用admob插页式广告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找答案,并且在目标C中找到了很多示例(Google开发人员文档等),并且很快就找到了一些答案,但是没有使用spritekit,并且是新手,我只是无法弥合这些教程中的空白,无法将其全部整合到我的项目中。老实说,我不太在乎广告是在应用启动还是在游戏结束时被调用,我很高兴能够调用广告时段,尽管我猜想在游戏启动时会更受欢迎。

I've been looking all over for an answer to this and I've found lots of examples in objective C (google developer docs, etc.) and some answers in swift, but not using spritekit, and being a novice, I just haven't been able to bridge the gaps in these tutorials to put it all together for my project. Honestly, I don't really care if the ad is called on app launch, or game over, I'd just be happy being able to call the ad period, though I guess on game launch is preferred.

可以肯定,我已经在gameviewcontroller.swift中正确设置了所有设置,但是我不知道该如何调用它。这是我gameviewcontroller.swift中的代码:

Pretty sure I've got it all set up correctly in gameviewcontroller.swift, but I don't know how to actually call it. Here is the code from my gameviewcontroller.swift:

import UIKit
import SpriteKit
import GoogleMobileAds

class GameViewController: UIViewController {

    var interstitial : GADInterstitial!

 func createAndLoadAd() -> GADInterstitial {

        var ad = GADInterstitial()
        ad.adUnitID = "ca-app-pub-4471013071748494/2980967718"
        var request = GADRequest()
        request.testDevices = [""]

        return ad

    }

除此之外……我也有这个if语句来调用广告,但是我不知道它应该在哪里……或者它是否完整(在本教程中,它位于一个按钮中,但是我自然需要它是自动的:

Beyond this ... I also have this if statement to call the ad, but I don't know where it should be ... or if it's complete (in the tutorial, it was in a button, but I need it to be automatic, naturally:

if (self.interstitial.isReady) {
            self.interstitial.presentFromRootViewController(self)
            self.interstitial = self.createAndLoadAd()
        }

对此有任何经验吗?如果有人可以通过让我知道在if语句上方放置该内容的方式来帮助我完成此操作,或者还有更多需要完成的工作,将非常感谢您的帮助。

Anyone have any experience with this? If anyone could help me complete this by letting me know where to place that above if statement, or if there is more that needs to be done ... your help will be much appreciated. Thank you.

推荐答案

好,所以我开始了一个新项目(Swift项目),即游戏项目

Ok so I started a new project (Swift project) which is Game Project

在GameViewController中:

In GameViewController:

class GameViewController: UIViewController
{
var interstitial = GADInterstitial()

override func viewDidLoad()
{
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? 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

        skView.presentScene(scene)



        let delayTime = dispatch_time(DISPATCH_TIME_NOW,
            Int64(5 * Double(NSEC_PER_SEC)))

        //Creation of the ad and its request
        self.interstitial = GADInterstitial()
        self.interstitial.adUnitID = "ca-app-pub-4798156420380453/7714267723"
        var request = GADRequest()
        // request.testDevices = [""]
        self.interstitial.loadRequest(GADRequest());//The method u were missing

        dispatch_after(delayTime, dispatch_get_main_queue())
        {
            self.showAd()
        }

    }
}



 func showAd()
 {
    if (self.interstitial.isReady)
    {
        self.interstitial.presentFromRootViewController(self)//Whatever  shows the ad
    }
}

这是我的第一个快速代码,所以不要判断我是否做错了什么
但是当我在±5秒后吃午餐时,我看到了广告
这是您的示例。

It is my first swift code so don't judge me if I did something wrong But when I lunch it after ±5 seconds i see the ad Here is your example.

我对Swift一点都不熟悉,但
要做的就是获取插页式广告放开时委托->并请求下一个插页式广告,因此在下一次您将获得新的广告

I am not familiar with Swift at all , but all you need to do is to get the interstitial delegate when it dismissed -> and request for the next interstitial, So in next time you will get the new AD

随意问什么!
P.S.不要忘记将admob ID更改为您的ID。

Feel free to ask anything ! P.S. Don't forget to change the admob ID to your ID.

这篇关于如何使用swift,spritekit和xcode调用admob插页式广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆