yellowlan yellowlan - 1 year ago 122
Swift Question

swift: How to call an ad made in ViewController in game scene

I have an interstitial ad declared in GameViewController and want to call it every few times my game ends. I followed a tutorial where I would call the ad by a button but I need to call from GameScene but I don't know how to call across classes. Could I use a global variable between all classes or something?
Basically I want to call func adButton() in GameScene or maybe there is some better work around.

Also, is there any way to implement reward ads or does anyone have any good tutorial like it?

Thanks very much.

func createAndLoadInterstitial() -> GADInterstitial {

let interstitial = GADInterstitial(adUnitID: " unit id ")
let request1 = GADRequest()
interstitial.delegate = self
request1.testDevices = [ kGADSimulatorID, "2077ef9a63d2b398840261c8221a0c9b" ]
return interstitial


func adButton() {

if (self.interstitial.isReady)
self.interstitial = self.createAndLoadInterstitial()

Answer Source

Use notifictionCenter to call the function

Add the first function in the viewDidLoad() and the second in the adButton()

In Swift 3

//In the gameviewcontroller
    NotificationCenter.default.addObserver(self, selector: #selector(YourFunctionNameHere), name: "NSNotificationCreate" as NSNotification.Name, object: nil)
//In the gameScene use this to call the ad "NSNotificationCreate" as NSNotification.Name, object: nil)

In Swift 2

//In the gameviewcontroller
   NSNotificationCenter.defaultCenter().addObserver(self, selector: "YourFunctionNameHere", name: "NSNotificationCreate", object: nil)

//In the gameScene use this to call the ad
   NSNotificationCenter.defaultCenter().postNotificationName("NSNotificationCreate", object: nil)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download