Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iAd in sprite kit game

I know there is at least one question asking how to integrate iAd into a sprite kit game, but that is not what I am looking for. I am looking for a portrait version of how to do it. There seem to be absolutely 0 tutorials online as to how to do this, so I came here. Can someone please tell me how to simply enable iAd in a Sprite Kit game? I have enabled canDisplayBannerAds and have used the originalContentView property for my UIView, but I keep getting a crash that says

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController originalContentView]: unrecognized selector sent to instance 0x10a0a7410'

any help would be appreciated, and here is my code in my view controller

- (void)viewWillLayoutSubviews 
{
[super viewWillLayoutSubviews];

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

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

self.canDisplayBannerAds = YES;

// Present the scene.
[skView presentScene:scene];
}
like image 667
user2277872 Avatar asked Feb 10 '14 05:02

user2277872


Video Answer


1 Answers

For those that don't understand, and those that are for future references, here is how I did this to my Sprite Kit game.

I had my game project created using the SpriteKit template in Xcode, then went to the project settings as so:

enter image description here

In the "Link Binary With Libraries" section, just make sure to hit the + button, and add the iAd framework.

After doing that, go to your view controller for your Sprite Kit game, and then type this:

// at the top 
#import <iAd/iAd.h>

// do this in .m, above @implementation
@interface YourViewControllerClassName () 
@property (nonatomic, strong) ADBannerView *banner;
@end

// after the implementation line
// if you're needing to do it horizontally, do:
- (void) viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    self.banner = [[ADBannerView alloc] initWithFrame:CGRectZero];
    self.banner.delegate = self;
    [self.banner sizeToFit];
    self.canDisplayBannerAds = YES;

    SKView *view = (SKView *)self.originalContentView;
    SKScene *scene = [[YourScene alloc] initWithSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];

    [view presentScene:scene];
}

If you're doing the iAd in just a normal, portrait, you can do the above code, but you can also just use - (void) viewDidLoad instead...

Now is the delegate methods for the iAd to appear...

Go to the code above the @implementation line, and edit it

// do this in .m, above @implementation
@interface YourViewControllerClassName () <ADBannerViewDelegate>
@property (nonatomic, strong) ADBannerView *banner;
@end

Now, go under the implementation line, and enter this:

// NOTE: THIS CODE CAME FROM APPLE MOSTLY
// I DID EDIT IT, BUT THE CREDIT GOES TO APPLE'S DOCUMENTATION
// ON IAD 
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error 
{
    if (banner.isBannerLoaded) {
       [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
       // Assumes the banner view is placed at the bottom of the screen.
       banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
       [UIView commitAnimations];
    } 
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner 
{
   if (!banner.isBannerLoaded) {
       [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];
    }
}

That is all that is required from the iAd to actually work in a SpriteKit game. I hope that I helped those that are going to read this.

like image 52
user2277872 Avatar answered Oct 31 '22 05:10

user2277872