How to add rewarded video ads to your SpriteKit game with Chartboost 📈

chartboost-ad

I hate ads in games. I hate them in apps. I hate them on the web. I hate them anywhere really. I’m sure you do too.

They’re annoying, they clutter the view, they obnoxiously clamour for your attention, and in app and game development they’re often an afterthought or the last thing added so they tend to feel out of place.

Unfortunately, in today’s market where nobody wants to pay even $1 for an app or game, developers are left with few options to try and gain some income from their work.

Video reward ads to me seem like a good compromise. Yes, you’re still showing the user an obnoxious ad, but you’re not shoving it in their face. When they see it, they’re ready for it and most importantly, you’re giving them the choice to view it.

So, if you’re interested in implementing this type of ad in your iOS SpriteKit game, here’s how (note: I’m using Chartboost in this example, but there are other options out there):

 

 

  • Once you’re logged in with your new account, create an App (iOS) and a Campaign. For details on how to do this, see this page.

 

  • On your Dashboard, select your app, go to App Settings, choose Basic Settings and note your App ID and App Signature:

 

chartboost-dashboard

 

 

  • Create a new Xcode project -> SpriteKit game.

 

  • Drag and drop the Chartboost.framework you extracted above into your new project project.

 

  • In your project settings, go to the Build Phases tab, and under Link Binary With Libraries add the following frameworks if you don’t have them yet: UIKit, CoreGraphics, StoreKit, Foundation:

 

chartboost-libs

 

  • We need to create a Bridging Header because Chartboost uses Objective-C code. In Xcode still, click File -> New -> File… and select Header File. Name the file YOUR_GAME_NAME-Bridging-Header.h where YourGameName is the name of your Xcode project.

 

  • Select this new file in Xcode, and add the following:

 

 

  • Under your project’s Build Settings, add the name of your Bridging Header file in the Objective-C Bridging Header field:

 

bridingheader

 

  • In your AppDelegate.swift, add the following in your application… DidFinishLaunchingWithOptions replacing “YOUR_APP_ID” and “YOUR_APP_SIG” with the ones you noted in Step 3.

 

 

  • Now, in our GameScene we’re going to add a “Watch Ad” button. When the user taps on it, we’ll fetch and display a video ad. Open your GameScene.swift file and delete all the placeholder stuff in didMoveToView and touchesBegan.

 

  • In didMoveToView, let’s create a simple SKLabelNode to use as a button.

 

 

  • Now let’s make sure we’re listening for this in touchesBegan. Add the following:

 

 

  • Let’s create the method that is called when our videoButton is tapped and tell Chartboost to show the video ad. Places this somewhere in GameScene.swift:

 

 

  • Launch your app in the iOS Simulator, tap the button and you should now have an ad!

 

videoad2

 

Delegates

 

But how do you know when the user finished watching the ad, or when they closed it, or if the ad even displayed at all? Luckily, Chartboost offers several delegate functions to handle these things. In your GameScene.swift, add the following:

 

 

Then, just add the following delegate methods (or only the ones you’ll use) at the bottom of your GameScene.swift class: