我想知道如何在展示我的GameOverScene时设置Admob插页式广告.我有什么办法只在游戏结束时才展示广告?我如何在Swift中实现它?
我是指这篇文章如何使用swift,spritekit和xcode调用admob interstitial广告?但我想知道如何在场景之间调用广告.
编辑 以下是我用来展示广告的代码
class GameViewController: UIViewController, GADInterstitialDelegate { var interstitial = GADInterstitial() var intersitialRecievedAd = false let defaults = NSUserDefaults.standardUserDefaults() override func viewDidLoad() { super.viewDidLoad() interstitial.delegate = self self.interstitial = createAndLoadAd() let timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "checkIfAdIsToBeDisplayed:", userInfo: nil, repeats: true) //Scene implementation, boring stuff, got nothing to do with the ads... ... } func checkIfAdIsToBeDisplayed(timer:NSTimer) { if defaults.boolForKey("adToBeShown") == true && intersitialRecievedAd == true { showInterstitial() defaults.setBool(false, forKey: "adToBeShown") intersitialRecievedAd = false } else { } } func interstitialDidReceiveAd(ad: GADInterstitial!) { intersitialRecievedAd = true } func createAndLoadAd() -> GADInterstitial { var ad = GADInterstitial(adUnitID: "...") ad.delegate = self let request = GADRequest() request.testDevices = ["..."] ad.loadRequest(request) return ad } func showInterstitial(){ if self.interstitial.isReady { self.interstitial.presentFromRootViewController(self) self.interstitial = createAndLoadAd() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Release any cached data, images, etc that aren't in use. } override func prefersStatusBarHidden() -> Bool { return true } }
此代码使用计时器来不断检查是否已呈现gameOverScene.当gameOverScene出现时,我分配true
给"adToBeShown"
bool.这不是最好的方法,所以有人能告诉我如何在呈现场景时直接调用广告吗?
为什么不使用NSNotificationCenter或委托来调用showInterstitial().
例如
在gameViewController中,在viewDidLoad中添加它
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showInterstitial"), name:"showInterAdKey", object: nil);
当您想要从您使用的场景中显示广告时
NSNotificationCenter.defaultCenter().postNotificationName("showInterAdKey", object: nil)
你也可以使用像我贴在GitHub上的帮手,这使得这个更容易和你的ViewController保持清洁 https://github.com/crashoverride777/Swift-2-iAds-and-AdMob-Helper