3
votes

I have added iAds to my Sprite Kit game with the following code:

In the viewController.h file

@property (strong, nonatomic) IBOutlet ADBannerView * adBannerView;

In the viewController.m file

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {

        // Create and configure the scene.
        SKScene * scene = [MenuScene sceneWithSize:skView.bounds.size];
        scene.scaleMode = SKSceneScaleModeAspectFill;

        _adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
        _adBannerView.delegate = self;
        [_adBannerView setFrame:CGRectMake(0, 0, 460, 320)];
        [self.view addSubview:_adBannerView];

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

This shows the iAd in every scene. Is there a way to hide the iAd in some of the scenes?

Apple's iAd Programming Guide says:

Only create a banner view when you intend to display it to the user. Otherwise, it may cycle through ads and deplete the list of available advertising for your application.

Is this at all possible with scenes?

3

3 Answers

4
votes

yes there is a way to hide the iAd in some of the scenes.

- (void)viewDidLoad
{

    [super viewDidLoad];

     //Add view controller as observer
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"hideAd" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = NO;
    skView.showsNodeCount = NO;

    // Create and configure the scene.
    SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
    self.canDisplayBannerAds = YES;

    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectOffset(adView.frame, 0, 0.0f);
    adView.delegate=self;
    [self.view addSubview:adView];

    self.bannerIsVisible=NO;  
}
//Handle Notification
- (void)handleNotification:(NSNotification *)notification
{ 
    if ([notification.name isEqualToString:@"hideAd"]) {
        [self hidesBanner];
    } else if ([notification.name isEqualToString:@"showAd"]) {
        [self showBanner];
    }
}

And in your scene in which you want to hide banner...

[[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil]; 
//Sends message to viewcontroller to show ad.

[[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil];  
//Sends message to viewcontroller to hide ad.
0
votes

Well, In your specific scene follow the Apple's guide(same place as your question) on this issue at the link below, look at the section that says "banner view best practices": https://developer.apple.com/library/ios/documentation/userexperience/conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html#//apple_ref/doc/uid/TP40009881-CH4-SW3

In Summary they say: "remove the banner view from the view hierarchy, set its delegate to nil"

0
votes

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

@protocol MySceneDelegate <NSObject>
- (void)hideAd;
@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.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    MyScene * scene = [MyScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

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

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

Then in the scene you can call the hideAd method of the view controller which was set as a delegate:

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

And remove the banner in hideAd method.

To hide the banner view, you should:

Resize your banner view's frame to be offscreen Resize your content view's frame to cover the space originally hosting the banner

Hope it helps.