I am making a game using Xcode and SpriteKit, I am integrating ads using admob and have all of the code working for the banner ads and have been able to get the interstitial ads to work as well. My problem is telling the game when to display the interstitial ad. The ad is in GameViewController but I need to call the showAd() method from GameScene.swift How would I go about calling a function from GameViewController in GameScene. Any help is appreciated, thanks in advance, Zach.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
In your GameViewController
, setup a notification observer in viewWillLayoutSubviews
like so:
override func viewWillLayoutSubviews() { NotificationCenter.default.addObserver(self, selector: #selector(self.showAd), name: NSNotification.Name(rawValue: "showAd"), object: nil) }
Then in your GameScene, call this when you want the function in GameViewController
to be run:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showAd"), object: nil)
Keep in mind that you need to have a function to be called inside your GameViewController
. showAd
is just a placeholder for whatever function you want to be run inside your GameViewController
.
Hopefully this helps!