2

I'm making a simple game and I would like to add an iAd at the top of the Game Over Screen. I could add it on to the UIViewController, but then it would show up while playing, which is something I don't want.

Is there a way to make the iAd appear only on a certain SKScene? Thanks for all your help!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
José María
  • 2,835
  • 5
  • 27
  • 42

1 Answers1

3

The most clean solution is to declare and implement a protocol to let the UIViewController know that it should hide the ad.

@protocol MySceneDelegate <NSObject>
- (void)hideAd;
- (void)showAd;
@end

@interface MyScene : SKScene
@property (weak) id <MySceneDelegate> delegate;
@end

View controller that shows the scene should implement a hideAd method and set itself as a delegate of the scene. Example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view, etc.
    ...

    // Set the delegate
    [scene setDelegate:self];

    // Present the scene.
    [skView presentScene:scene];
}

Then in the scene you don't want to show an ad, you can call the hideAd method of the view controller which was set as a delegate:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    /* Called when a touch begins */

    if ([_delegate respondsToSelector:@selector(hideAd)])
    {
        [_delegate performSelector:@selector(hideAd)];
    }
}

You can tell the view controller to show the ad in the same way. Hope it helps.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • Hi By any chance could you show this code working in a project? I've tried adding the protocols but it's still showing the ad when I'm trying to keep it hidden. – uplearned.com Dec 06 '14 at 13:47