如何使用 swift、spritekit 和 xcode 调用 admob 插页式广告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29838262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:03:54  来源:igfitidea点击:

How to call admob interstitial ad using swift, spritekit and xcode?

iosxcodeswiftadmobinterstitial

提问by user3147770

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.

我一直在寻找这个问题的答案,我在目标 C(谷歌开发人员文档等)中找到了很多例子,并且在 swift 中找到了一些答案,但没有使用 spritekit,作为一个新手,我只是没有无法弥补这些教程中的空白,将它们整合到我的项目中。老实说,我并不在乎广告是在应用启动时调用还是在游戏结束时调用,我只是很高兴能够调用广告期,尽管我想在游戏启动时调用是首选。

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:

很确定我已经在 gameviewcontroller.swift 中正确设置了所有内容,但我不知道如何实际调用它。这是我的 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

    }

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 语句来调用广告,但我不知道它应该在哪里......或者它是否完整(在教程中,它在一个按钮中,但我需要它自动,自然:

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

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.

有人对此有经验吗?如果有人可以通过让我知道在 if 语句上方放置该语句的位置来帮助我完成此操作,或者是否还有更多工作要做……您的帮助将不胜感激。谢谢你。

回答by ColdSteel

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

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

In GameViewController:

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

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 代码,所以如果我做错了什么,请不要评判我但是当我在 ±5 秒后吃午饭时,我看到了广告 这是你的例子。

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

我对 Swift 一点都不熟悉,但是你需要做的就是在它关闭时获得插页式委托​​ -> 并请求下一个插页式广告,所以下次你会得到新的 AD

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

随意问任何事情!PS不要忘记将admob ID更改为您的ID。

回答by Piotr

My solution based on above comments was the following:

我基于上述评论的解决方案如下:

1) Add this in the GameViewController.swift

1) 在 GameViewController.swift 中添加这个

protocol AdmobInterstitialDelegate{
    func showInterstitial()
}
...
// add this delegate in GameViewController definition
class GameViewController: UIViewController, GADInterstitialDelegate, AdmobInterstitialDelegate {
...
// and assign it to the GameScene class when you create its instance
     let scene = GameScene()
     scene.adDelegate = self
// do not forget to implement the delegated method itself
    func showInterstitial(){
        if (interstitial!.isReady) {
            interstitial!.presentFromRootViewController(self)             
        }
    }

and then you can use it in GameScene

然后你可以在 GameScene 中使用它

class GameScene: SKScene {
....
    var adDelegate: AdmobInterstitialDelegate?
....
// and finally - show the ad
    override func didMoveToView(view: SKView) {
        self.adDelegate?.showInterstitial();
        ....

hope this helps.

希望这可以帮助。

回答by Sajjad Aims

keep the if condition in viewDidAppear it will automatically execute

在 viewDidAppear 中保留 if 条件,它会自动执行

override func viewDidAppear(animated: Bool) {
        if (interstitial.isReady) {
        interstitial.presentFromRootViewController(self)
        self.interstitial = self.createAndLoadInterstitial()
        }