user2255273
user2255273

Reputation: 958

Adding iAds to Sprite Kit Landscape

I have the following code in my View Controller:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

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

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

        self.canDisplayBannerAds = YES;

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

When I run the application it crashes immediately. I get the following error message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView scene]: unrecognized selector sent to instance

I can't use SKView *skView = (SKView *)self.originalContentView; because the app is in landscape mode. Is there a way to display iAds in a Landscap Sprite Kit game?

EDIT:

I just added this code to the view controller, but I get the same results..

#pragma mark iAd Delegate Methods

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [banner setAlpha:1];
    [UIView commitAnimations];
}

Upvotes: 1

Views: 1029

Answers (3)

iC7Zi
iC7Zi

Reputation: 1658

None of the above answers works for me. Below code is tested in iOS 7 and 8 and works just fine.

Add below lines in header file

#import <iAd/iAd.h>

@interface GameViewController : UIViewController<ADBannerViewDelegate>{

    //iAd
    ADBannerView *adView;

}

In Implementation file .m add below code

#import "GameViewController.h"
#import "GameScene.h"

@implementation GameViewController

-(void)viewWillLayoutSubviews{

    [super viewWillLayoutSubviews];

    SKView *skView;

    if (self.originalContentView) {

        skView = (SKView *)self.originalContentView;

    }

    if (!skView.scene) {

        //[skView setShowsDrawCount:YES];
        //[skView setShowsFPS:YES];
        //[skView setShowsNodeCount:YES];

        //Improve Performance
        skView.ignoresSiblingOrder = YES;

        GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
        [scene setScaleMode:SKSceneScaleModeFill];

        [skView presentScene:scene];

    }

}

- (void)awakeFromNib{

    [super awakeFromNib];

    CGRect screenRect = [[UIScreen mainScreen] bounds];

    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectMake(0, 0, screenRect.size.width, adView.frame.size.height);
    adView.delegate=self;
    [self.view addSubview:adView];
}

//iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {

    [adView setAlpha:1.0];
    NSLog(@"Show Ad");

}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {

    [adView setAlpha:0];
    NSLog(@"Hide Ad");

}

//

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

-(BOOL)prefersStatusBarHidden{

    return YES;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

@end

Upvotes: 1

Mick MacCallum
Mick MacCallum

Reputation: 130193

You can solve this without having to manually configure the ad banner. You do still have to use the original content view, but making it work correctly is just a matter of putting everything together in the right stages of the view controller's life cycle. All you have to do is enable canDisplayBannerAds: in awakeFromNib. Then set the SKView you create to either the view controller's view or originalContentView depending on the existence of the original content view.

- (void)awakeFromNib
{
    [super awakeFromNib];

    [self setCanDisplayBannerAds:YES];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    SKView *skView = nil;

    if (self.originalContentView) {
        skView = (SKView *)self.originalContentView;
    }else{
        skView = (SKView *)self.view;
    }

    [skView setShowsDrawCount:YES];
    [skView setShowsFPS:YES];
    [skView setShowsNodeCount:YES];

    SKScene *scene = [MyScene sceneWithSize:skView.bounds.size];
    [scene setScaleMode:SKSceneScaleModeFill];

    [skView presentScene:scene];
}

Upvotes: 1

prototypical
prototypical

Reputation: 6751

I ran into this issue and solved it via creating an ABBannerView property and adding that as a subView.

In my ViewController class :

adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.delegate = self;
[adView setFrame:CGRectMake(0, 0, 1024, 768)]; // set to your screen dimensions
[self.view addSubview:adView];

Important to NOT set the canDisplayBannerAds property of your view controller.

I believe what is happening is that if you do set the canDisplayBannerAds property to true, the view is modified and is no longer compatible with a SKView, and no longer has a scene property.

I did have to set the frame, so that the dimensions were correct otherwise it was portrait.

Upvotes: 3

Related Questions