Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

Adding iAds to a Sprite Kit App

Adding iAds to a Sprite Kit App
July 30, 2014 Justin

Lets see if I can remember the steps to adding iAds to a Sprite Kit (Objective C) project. First be sure the iAds framework has been imported.

Step 1.

Adding iAds to Sprite Kit with Objective C

Step 2. Then head over to your ViewController.h file.

 

Notice we’ve added an #import statement and the <ADBannerViewDelegate>

Step 3. I declared…

…in the @interface portion of the ViewController.m file. You could also keep track of whether or not the ad banner is visible. Which is why in the pic below you see the iadsBannerIsVisible bool.

Adding iAds to Sprite Kit with Objective C

 

Step 4. Then in the viewWillLayoutSubviews statement I did some testing to see if my app was purchased or not, and also whether the app was set to show a thin banner or square one. In my app I had a property list setting to show a thin banner

Step 5. Time for the meat of this. The showThinBanner method….

 

That code puts the theBanner at the bottom of the screen. If you want the banner at the top, just remove the line of code that sets the frame property.

Step 6: The rest of these methods run automatically without you calling them. The bannerViewDidLoadAd statement occurs when the banner loads….

The bannerViewActionShouldBegin occurs when tapping the ad and it takes over…

The bannerViewActionDidFinish runs when the banner is done being fullscreen…

 

The next method runs when the app failed to receive an ad. If the banner is visible (the placeholder for the ad) it animate out of the way. In this case, it moves down off-frame.

Step 7. Finally if you want remove the ads yourself, you can call this method.  Apple doesn’t recommend hiding your ads unnecessarily. Of course if someone bought an in-app purchase specifically to kill the advertising, then you’ll want to get those iAds removed as quick as possible. You’ll probably need to create an NSNotification to listen out for the purchase, then call this…

 

 

Justin Dike is the owner and lead developer at CartoonSmart.com. He's taught hundreds of hours of game development video tutorials, covering everything from coding to art. These days he's working mostly on Swift and Sprite Kit tutorials, which often lead into highly polished iOS / tvOS Starter Kits, which require no programming at all, but are capable of making nearly any type game! Yeah, you read that write. You can also find Justin at the official CartoonSmart Podcast

1 Comment

  1. Nick 3 years ago

    Hello. Thank you for the tutorial. I re-wrote my iAd code to yours, changing only some things slightly. So far, everything works as it had with my previous i Ad code. The only real change to the code was having my Banner appear at the top & not the bottom. But I still have 3 questions: 1) Is BOOL adOnTop; accidentally forgotten to be deleted? 2) So long as I delete all the “banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);” then my Banner will go away by it self fine, right? 3) Have you ever had an iAd bug where a 2nd banner would appear & take over view space – as in forcefully push the game view out of the way, forcing it to be “squeezed” (this happened with my previous iAd code & my NSNotification calling on a method to hide the Banner only worked for the top one. The forceful bottom one didn’t go away…)?

Leave a reply

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com