Swift 如何添加Admob插入式广告
我试图在Swift SpriteKit游戏中添加一个间隙广告,我看到的大多数问题/解决方案都与横幅视图有关,而不是admob广告的间隙显示 我所做的是将谷歌的所有框架和示例代码(如本文所述)添加到我的GamveViewController.swift中Swift 如何添加Admob插入式广告,swift,sprite-kit,admob,Swift,Sprite Kit,Admob,我试图在Swift SpriteKit游戏中添加一个间隙广告,我看到的大多数问题/解决方案都与横幅视图有关,而不是admob广告的间隙显示 我所做的是将谷歌的所有框架和示例代码(如本文所述)添加到我的GamveViewController.swift中 import UIKit import GoogleMobileAds class ViewController: UIViewController { var interstitial: GADInterstitial! over
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)
}
}
然后我想调用函数在GameSecene.swift中显示广告,这会导致一个错误,我不知道为什么
if (self.interstitialAd.isReady) {
self.interstitialAd.presentFromRootViewController(self)
}
我得到的错误代码是:
1.使用未声明的类型“gadinertial”
2.“游戏场景没有成员间隙
似乎我需要连接GameViewController和游戏场景,但我不知道如何连接。有人伸出援助之手吗?你不能只在游戏场景中展示广告,你需要一个viewController 最简单的方法是将游戏场景中的代码放入ViewController 因此,在ViewController中移动/或创建func,如下所示
func showInterAd() {
/// code to present inter ad
}
然后,您可以使用NSNotificationCenter之类的工具将消息从游戏场景转发到ViewController
仍在ViewController中,请在viewDidLoad中添加观察者
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(showInterAd), name: "ShowAd", object: nil)
而不是在游戏场景中,当你想显示广告发布通知时
NSNotificationCenter.defaultCenter().postNotificationName("ShowAd", object: nil)
或者,如果您想要更干净、更可重用的解决方案,我在gitHub上有一个助手
感谢您的帮助-通知中心似乎解决了我的问题。由于我对开发总体上是全新的,因此对快速开发也是全新的——不幸的是,我对通知中心不太适应。希望能找到一些好的资源。你有什么建议吗?嘿,不客气。这应该会有帮助。让我知道进展如何