我每次我的游戏过渡到游戏场景时都试图向间隙广告展示。但是,只有当我将其初始化函数放在我的ViewDidload((函数中时,AD才会出现。我在游戏中设置了一个通知中心,并且在输入游戏场景时试图发送通知,以触发初始化广告的功能,但没有解决问题。我想知道如何在任何给定时间从场景触发它,而不是在启动应用程序时立即显示它,这就是将其放在视图控制器的ViewDidload功能中。
在我的GameViewController中是这两个功能:
public func initAdMobInterstitial() {
adMobInterstitial = GADInterstitial(adUnitID: AD_MOB_INTERSTITIAL_UNIT_ID)
adMobInterstitial.delegate = self
let request = GADRequest()
request.testDevices = ["ddee708242e437178e994671490c1833"]
adMobInterstitial.load(request)
}
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
ad.present(fromRootViewController: self)
}
在这里,我已经评论了initadmobinterstitial,但是当毫无疑问时,广告弹出并正常工作。该应用首次启动后,此弹出窗口就会发生。
override func viewDidLoad() {
super.viewDidLoad()
//initAdMobInterstitial()
initAdMobBanner()
NotificationCenter.default.addObserver(self, selector: #selector(self.handle(notification:)), name: NSNotification.Name(rawValue: socialNotificationName), object: nil)
let scene = Scene_MainMenu(size: CGSize(width: 1024, height: 768))
let skView = self.view as! SKView
skView.isMultipleTouchEnabled = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .aspectFill
_ = SGResolution(screenSize: view.bounds.size, canvasSize: scene.size)
skView.presentScene(scene)
}
现在,在我的一个场景中,标题为"游戏",我希望广告弹出。我希望每次展示场景时都会出现,因此每次玩家都输掉并击中游戏时。使用您可以在我的视图控制器类中看到的通知中心,我尝试发送通知并处理...
override func didMove(to view: SKView) {
self.sendNotification(named: "interNotif")
}
...通过此功能,也可以在视图控制器类
中找到func handle(notification: Notification) {
if (notification.name == NSNotification.Name(rawValue: interstitialNotificationName)) {
initAdMobInterstitial()
}
}
也要注意,在我的视图控制器中,我声明了与字符串" internotif"等于匹配的通知。
加载后不会立即出现GADInterstitial
。您的通知函数应呈现。然后,一旦用户驳回了广告请求,另一个。例如:
override func viewDidLoad() {
super.viewDidLoad()
// Load the ad
initAdMobInterstitial()
}
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
// Do not present here
// ad.present(fromRootViewController: self)
}
func handle(notification: Notification) {
if (notification.name == NSNotification.Name(rawValue: interstitialNotificationName)) {
// Check if the GADInterstitial is loaded
if adMobInterstitial.isReady {
// Loaded so present it
adMobInterstitial.present(fromRootViewController: self)
}
}
}
// Called just after dismissing an interstitial and it has animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
// Request new GADInterstitial here
initAdMobInterstitial()
}
有关GADInterstitialDelegate
AD事件的完整列表,请参阅ADMOB iOS AD事件。