2

I'm currently trying to implement IAP into my SKScene like this Swift Sprite Kit In App Purchase but I'm having a problem figuring out how to set self.canDisplayBannerAds to false from my skscene, The code I used did nothing. Any help would be appreciated.

 func removeAds() {

        let viewController: GameViewController = GameViewController()

        viewController.canDisplayBannerAds = false
    }
Community
  • 1
  • 1
Daniel Mihaila
  • 585
  • 1
  • 6
  • 13

1 Answers1

2

The reason why your code isn't working is because you are creating a NEW GameViewController and setting canDisplayBannerAds on that.

You need to keep a reference to your original GameViewController which you should be able to access from within your scene via your scenes SKView.

If you don't want to subclass your SKView you can use the following to get your current viewController.

if let viewController = view.nextResponder as? GameViewController /* or whatever your VC is */ {
    viewController.canDisplayBannerAds = false
}

If your SKView is a subview change view.nextResponder to view.superview.nextResponder.

Alternatively you can use Notification Center to send a message to your GameViewController

In your GameViewController.

override func viewDidAppear(animated: Bool) {
     super.viewDidAppear(animated)
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "turnOffAds", name: "TurnOffAdsNotification", object: nil)
}

func turnOffAds() {
     self.canDisplayBannerAds = false
}

Somewhere in your SKScene:

NSNotificationCenter.defaultCenter().postNotificationName("TurnOffAdsNotification", object: nil)
Beau Nouvelle
  • 6,962
  • 3
  • 39
  • 54
  • I understand. How could I go about doing this? I wanted to use self.view?.canDisplayBannerAds = false but this doesn't work because apparently 'value of type SKView has no members canDisplayBannerAds' – Daniel Mihaila Oct 17 '15 at 02:34
  • posted some code, give that a go, haven't tested it, but should work. – Beau Nouvelle Oct 17 '15 at 02:42
  • Sure you have your vc set as GameViewController? What doesn't work? – Beau Nouvelle Oct 17 '15 at 03:00
  • yea it's GameViewController. I don't get any errors, It just doesn't remove the ads – Daniel Mihaila Oct 17 '15 at 03:05
  • I don't know what your view hierarchy is, so it's difficult to know what to give you. The only other suggestion I have is to use NSNotificationCenter and just post a notification for when you want to remove those ads, and have a listener in your GameViewController. – Beau Nouvelle Oct 17 '15 at 03:07
  • And to receive the notification what would I have to do? Sorry I'm very unfamiliar with this code – Daniel Mihaila Oct 17 '15 at 03:57
  • You add the listener to your GameViewController like the above code. The `selector` is the method you want to call. The `name` is the name of the notification you're listening for. – Beau Nouvelle Oct 17 '15 at 03:59