hagope
hagope

Reputation: 5531

Interstitial iAd within SKScene in iOS Sprite Kit

The sample iAd code for interstitials uses the main ViewController to present the full-screen interstitial ad.

However, I would like to be able to trigger the ad from the SKScene in my iOS Sprite Kit game.

I can easily do so in the main view controller (ViewController.m) like so:

[self requestInterstitialAdPresentation];

However, I've tried to access the root view controller which sets up the scene in the viewDidLoad method of :

SKView * skView = (SKView *)self.view;
SKScene * scene = [SKScene sceneWithSize:skView.bounds.size];
[skView presentScene:scene];

In SKScene.m I've tried this:

[self.view.window.rootViewController requestInterstitialAdPresentation];

and X-Code throws this:

No visible @interface for 'UIViewController' declares the selector 'requestInterstitialAdPresentation'

This is probably something simple, basically I need to access the ViewController object that created the SKView and SKScene so I can send the requestInterstitialAdPresentation message to that object (and display the ad).

Upvotes: 0

Views: 1411

Answers (2)

Benny Abramovici
Benny Abramovici

Reputation: 582

You can use simple notification to communicate back to your viewController.In your viewController:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestInterstitialAdPresentation) name:@"showAdd" object:nil];

Then in your scene post the notification whenever you want your ad presented. In your SKScene:

[[NSNotificationCenter defaultCenter] postNotificationName:@"showAdd" object:nil];

Hopes this helps

Upvotes: 1

Joshua Pokotilow
Joshua Pokotilow

Reputation: 1233

Why don't you pass an instance of your view controller that implements requestInterstitialAdPresentation to your SKScene instance? That way, you can reference the VC directly rather than having to go through the window, which seems to be error-prone.

If you're going to take this advice — without knowing too much about your codebase — I would hazard a guess that it may make sense to create a delegate interface for the scene and to implement it in the view controller. The interface would look something like:

@protocol AdDelegate
- (BOOL)requestInterstitialAdPresentation;
@end

As far as understanding why your root view controller doesn't implement requestInterstitialAdPresentation, I suppose your best bet would be to debug the application and see what's going on. My suspicion is that you're dealing with a container view controller that wraps your main VC, or the scene you're presenting shows up in a different window.

Upvotes: 3

Related Questions