I'm using the MoPub banner ad, I added the following code to my View Controller's viewDidLoad:
self.adView.delegate = self
self.adView.frame = CGRectMake(0, self.view.bounds.size.height - MOPUB_BANNER_SIZE.height,
MOPUB_BANNER_SIZE.width, MOPUB_BANNER_SIZE.height)
self.view.addSubview(self.adView)
self.adView.loadAd()
But it makes the ad visible in all the scenes when I only want it to be visible in the main menu scene.
How do I remove the ad in the scenes that I don't want it to be?
This might not be the best way to do this, but it is probably the simplest. You could use an
NSNotification
to broadcast a message to yourViewController
whenever you wish to show or hide you banner.For instance if you add an "observer" in your
ViewController
oninit
orviewDidLoad
:To make the
ViewController
listen for a message called"hideAd"
and then execute a method calledhideBannerAd
.Then implement this method:
Be sure to remove the observer on
deinit
, this isn't likely to be an issue what with the persistance of aViewController
inSpriteKit
but it's good practice.Then, when you want to show or hide the view, for example on a scene transition or game over method, you can implement this
hideBannerAd
method by triggering the observer using:And the banner should hide. This can then be repeated for a similar
showBannerAd
method by setting thehidden
property tofalse
, or you can have a single method that simply toggles the hidden property usingadView.hidden = !adView.hidden
.I hope this helps.