Use the AdMob Helper for Rewarded Ads

Similar to what you did in Use the AdMob Helper for Banner Ads, you’ll modify the AdMobHelper.swift file to support your rewarded ads.

Open the AdMobHelper.swift file, and in the AdMobHelper struct, add the following new static properties:

 static​ ​var​ rewardAdReady = ​false
 static​ ​let​ rewardAdID = ​"ca-app-pub-3940256099942544/1712485313"​ ​// test

Once again, the example here uses the Google test ID, but you’re welcome to use the ID you set up earlier—provided you follow Google’s recommendations for testing ads.

Next, add the file-private property below the _adBannerView property to hold the rewarded ad object:

 fileprivate​ ​var​ _rewardedAd: ​GADRewardedAd​?

Finally, add the computed property to the GameViewController extension; place it below the existing adBannerView property:

 var​ rewardedAd: ​GADRewardedAd​? {
 get​ {
 return​ _rewardedAd
  }
 set​(newValue) {
  _rewardedAd = newValue
  }
 }

You’re now ready to set up rewarded ads in Gloop Drop.

Setting up Rewarded Ads

Still inside the AdMobHelper.swift file, and below the hideBanner(_:) method, add the following new method that initializes the rewarded ads object:

 // Set up Rewarded Ads
 func​ ​setupRewardAdsWith​(id: ​String​) {
 AdMobHelper​.rewardAdReady = ​false​ ​// reset the ready flag
 
  rewardedAd = ​GADRewardedAd​(adUnitID: id)
  rewardedAd?.​load​(​GADRequest​()) { error ​in
 if​ ​let​ error = error {
 print​(​"*********************** ​​(​error.localizedDescription​)​​"​)
  } ​else​ {
 print​(​"*********************** Reward Ad Loaded OK!"​)
 AdMobHelper​.rewardAdReady = ​true
  }
  }
 }

Similar to how banner ads were initialized in Setting up the Banner Ad View, this method resets the AdMobHelper.rewardAdReady static property and initializes a GADRewardedAd object. It then stores that object in the rewardedAd property.

You’re almost ready to show rewarded ads, but first, you need to do a few more things.

Preparing to Show Rewarded Ads

The way rewarded ads work is that the view responsible for playing the video needs to be presented from a view controller—in this case, the GameViewController. While you can handle a request like this in a few different ways, you’ll create a new custom GameScene protocol that the GameViewController will use.

Open the GameScene.swift file and add the following code to the top of the file; place it below the import statements and above the class declaration:

 protocol​ ​GameSceneDelegate​: ​AnyObject​ {
 func​ ​showRewardVideo​()
 }

This is the method you’ll call from the delegate (the GameViewController).

After that, add a new property to the GameScene class, like so:

 weak​ ​var​ gameSceneDelegate: ​GameSceneDelegate​?

You’ll use this property to store the delegate.

Finally, in the touchDown(atPoint:) method, locate the // TODO: Add call to gameSceneDelegate?.showRewardVideo() comment and remove the first part of the comment that reads // TODO: Add call to. When you’re done, you’ll end up with this:

 gameSceneDelegate?.​showRewardVideo​()

This line of code routes the call to showRewardVideo() from the GameScene to the delegate. Speaking of which, you still need to set up the delegate so that you can show the rewarded ads.

Setting up the Delegate and Showing Rewarded Ads

Open the GameViewController.swift file, and below the line that reads let scene = GameScene(size:CGSize(width: 1336, height: 1024)), add this:

 scene.gameSceneDelegate = ​self

This code sets the gameSceneDelegate property in the GameScene class, but it also throws the following error:

images/UsingAdsToIncreaseRevenue/ads-protocol-error.png

To fix that error, go to the bottom of the GameViewController.swift file and add a new extension that handles the GameScene protocol method:

 extension​ ​GameViewController​: ​GameSceneDelegate​ {
 func​ ​showRewardVideo​() {
 if​ rewardedAd?.isReady == ​true​ {
  rewardedAd?.​present​(fromRootViewController: ​self​, delegate:​self​)
  }
  }
 }

This method first verifies that a rewarded ad is ready for presentation; if it is, the view controller presents it.

The only thing left to do now is update the rewarded ad delegate methods. But first, you need to add a few custom notifications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset