6

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];
}
user2277872
  • 2,963
  • 1
  • 21
  • 22
  • it's working now. I wasn't linking the actual iAd framework to my application. I did that, and then i just imported iAd/iAd.h and now it works. Thank you for the advice :) – user2277872 Feb 10 '14 at 05:56
  • Does this not distort or resize your scenes? – Maccle415 Feb 20 '14 at 07:28
  • in a way, yes it does. If you want to know how to make it so it won't, go here: https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/iAd_Guide/BannerAdvertisements/BannerAdvertisements.html#//apple_ref/doc/uid/TP40009881-CH3-SW2 – user2277872 Feb 20 '14 at 15:26
  • What I done is I just dragged the iAd view onto the storyboard and do it the normal way. That puts the ad on all scenes, so I had to access the main scene from each other scene to change the main scenes userData or tag and the use a timer to check the the value every couple of seconds and then call a method to display or remove the ad. – Maccle415 Feb 21 '14 at 06:38
  • My content is still distorted. What did you do specifically in the iAd programming guide you linked to? I'm pretty new to obj-c and iOS. – HeliNinja Feb 21 '14 at 19:14
  • 1
    I did what you see above in the code. I have done some research though on the right ways of using iAd, and it seems that you should create an ADBannerView *bannerView; type variable, and in the init method, you should set the delegate to self. and then bannerView = [[ADBannerView alloc] initWithFrame:CGRectZero]; then in the delegate methods, load the frame to being 44 or something like that. just check out the apple documentation and it will have code already given to you. – user2277872 Feb 22 '14 at 00:08
  • What do you mean by you were not "linking the actual iAd framework to my application"? I have the exact same code as you and am getting the exact same error. I imported iAD/iAD.h and am still getting the following error. -[ViewController originalContentView]: unrecognized selector sent to instance 0x14689c30 – user3438986 Mar 30 '14 at 19:29
  • I'll put an answer up – user2277872 Apr 23 '14 at 12:32

1 Answers1

10

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.

user2277872
  • 2,963
  • 1
  • 21
  • 22
  • Nicely explained step by step – chanpkr Jul 23 '14 at 02:48
  • thanks. I had searched for weeks on how to accomplish the task of using iAd in sprite kit. I made this for all those in the same boat i was in – user2277872 Jul 23 '14 at 05:27
  • 1
    Sorry for asking such question but, what should you do to put the ads on top of the screen instead of on the bottom? – chanpkr Jul 23 '14 at 16:34
  • 1
    I'm not sure if that's something that you would really want to do, but all you would have to do is instead of self.banner = [[ADBannerView alloc] initWithFrame:CGRectZero]; you would say self.banner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];and that should work for you. Don't apologize for the question though, everyone has their troubles with iAd..:) – user2277872 Jul 24 '14 at 22:40
  • In addition, I did have to add `[self.view addSubview:self.banner]` in the viewWIllLayoutSubviews method in order for the ad to actually display on my sprite kit scene. – Friendly King Mar 08 '16 at 16:27