Search code examples
iosobjective-cxcodeios7iad

How to stop iAds from skewing the SKScene in xcode?


So I have added iAds to my application and I have done this very simply. By importing iAds into the ViewController.h and putting the following code into the ViewController.m file:

self.canDisplayBannerAds = YES;

This works, however, when I the add loads the screen is squished. Where all of the images and sprites become shorter.

Is there any way the add can just be overlaid on top of the SKScene? so that is does not affect the elements below – so that it stops the squishing?

Also, is there a way to only have the banner ads run in certain SKScenes or when a BOOL is changed from YES to NO?

UPDATED

Code from my project:

#import "XYZViewController.h"
#import "XYZMenuScene.h"
#import <iAd/iAd.h>

@implementation XYZViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

    //iAds Enabled
    self.canDisplayBannerAds = YES;

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

This is all I have in terms of the iAds. It makes the entire scene push upwards to make room for the ad banner. Where I want it to just be over the scene and not push anything. So it covers, instead of pushes.


Solution

  • To overlay you can set the Z position of the banner higher than the rest of the content by default it's 0.

    eg:

        banner.layer.zPosition=2;
    

    but I believe the iAd framework has built in method thats you can override to control when the ads must display and when they shouldn't and give you much more control. Specific methods are called when the ad is available,and like you said you make the ads appear only when a boolean value is 1 or 0.

    eg:

      - (void)bannerViewDidLoadAd:(ADBannerView *)banner
       {
    
      if(showADS==1)
      {
    if (!self.bannerIsVisible)
    {  
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
     // Assumes the banner view is just off the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
        [UIView commitAnimations];
        self.bannerIsVisible = YES;
    }
    }
     else
      {NSlog(@"No ads");}
    
    }
    

    check apple's documentation regarding this,they are really helpful https://developer.apple.com/library/ios/documentation/userexperience/Conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html

    But let give a complete example. The banner instance is only available in you iAd Methods not outside them. In your view did load method:

        - (void)viewDidLoad
    
        { 
    
         self.canDisplayBannerAds=YES;
    
         }    
    

    This method is called when the ad is loaded,in this method specify the position of the ad

        - (void)bannerViewDidLoadAd:(ADBannerView *)banner
          {
             banner.layer.zPosition=2;
            /*do all the additional like checking is the boolean is 1 or 0 etc.*/ 
    
          }
    

    If you are working with iAd and sprite kit,you need to call methods in your view controller from the scene class to do all the ad work.

    Suppose you have a method named checkAD in your view controller that checks if the ad should shown or not etc..

    You can call this method from your scene class by using this code

    if([self.delegate respondsToSelector:@selector(checkAD)]){
    
        [self.delegate checkAD];
    }