我试图在一个Swift SpriteKit游戏中添加一个间隙广告,我看到的大部分问题/解决方案都与横幅视图有关,而不是admob广告的间隙显示。
我所做的是将google的所有框架和示例代码添加到我的GamveViewController.swift中,如这里所描述的
import UIKit
import GoogleMobileAds
class ViewController: UIViewController {
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
let request = GADRequest()
// Requests test ads on test devices.
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
interstitial.loadRequest(request)
}
}然后我想调用函数在我的GameScene.swift中显示广告,这会导致一个错误,我不知道为什么
if (self.interstitialAd.isReady) {
self.interstitialAd.presentFromRootViewController(self)
}我得到的错误代码是: 1.使用未声明的类型"GADInerstittial“2."GameScene没有成员间”。
似乎我需要连接我的GameViewController和游戏场景,但我不知道如何。有人伸出援手吗?
发布于 2016-05-08 15:46:23
您不能只在gameScene中显示广告,您需要一个viewController。
最简单的方法是将GameScene中的代码放入ViewController中。
因此,在ViewController中移动/或创建一个函数,如下所示
func showInterAd() {
/// code to present inter ad
}而不能使用类似于NSNotificationCenter的东西将消息从GameScene转发到ViewController。
仍然在ViewController中在viewDidLoad中添加一个观察者
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(showInterAd), name: "ShowAd", object: nil)当您想要显示广告发布通知时,要比在您的GameScene中。
NSNotificationCenter.defaultCenter().postNotificationName("ShowAd", object: nil)或者,如果您想要一个更干净、更可重用的解决方案,那么我在gitHub上有一个助手。
https://github.com/crashoverride777/Swift-iAds-AdMob-CustomAds-Helper
https://stackoverflow.com/questions/37100474
复制相似问题