I've implemented rewarded videos in my app using firebase. I have done everything necessary according to this page: https://developers.google.com/admob/ios/rewarded-video
I have imported the following (regarding the ads):
import Firebase
import GoogleMobileAds
class ViewController: UIViewController, MFMailComposeViewControllerDelegate, GKGameCenterControllerDelegate, GADInterstitialDelegate, GADRewardBasedVideoAdDelegate, UIAlertViewDelegate {...}
So I set up the delegate and I load an ad:
rewardBasedVideo = GADRewardBasedVideoAd.sharedInstance()
rewardBasedVideo.delegate = self
if !adRequestInProgress && rewardBasedVideo?.isReady == false {
GADRewardBasedVideoAd.sharedInstance().delegate = self
rewardBasedVideo?.load(GADRequest(),
withAdUnitID: "ca-app-pub...")
adRequestInProgress = true
}
I also call the ad when I press a button:
if rewardBasedVideo?.isReady == true {
rewardBasedVideo?.present(fromRootViewController: self)
if !adRequestInProgress && rewardBasedVideo?.isReady == false {
rewardBasedVideo?.load(GADRequest(),
withAdUnitID: "ca-app-pub-3940256099942544/1712485313")
//ca-app-pub-8020832369894951/1361675226
adRequestInProgress = true
}
Then, I have all the functions for the delegate:
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
didRewardUserWith reward: GADAdReward) {
print("You eaerned $\(abbreviateNumber(num: (moneyPerSecond * 360.0) as NSNumber))")
money = UserDefaults.standard.double(forKey: moneyKey)
money += moneyPerSecond * 360.0
UserDefaults.standard.set(money, forKey: moneyKey)
}
func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
adRequestInProgress = false
print("Reward based video ad is received.")
}
func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
print("Opened reward based video ad.")
}
func rewardBasedVideoAdDidStartPlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
print("Reward based video ad started playing.")
}
func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
print("Reward based video ad is closed.")
}
func rewardBasedVideoAdWillLeaveApplication(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
print("Reward based video ad will leave application.")
}
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
didFailToLoadWithError error: Error) {
print("Reward based video ad failed to load.")
}
The following happens: I can load an ad (after which the rewardBasedVideoAdDidReceive runs), I can watch the ad and close the ad. But, the function that should reward the user does not run. I need to know why this doesn't run correctly, so I can properly reward the user.
Did you ever figure out the issue? I think I just ran into the same issue.
I did exactly what you did in this section "I also call the ad when I press a button:". I tried to load another ad upon pressing "play ad" if the ad was not ready.
After you present the ad, the "isReady" will be set to nil. Therefore, in your situation, you will always be loading another ad right after presenting one. And for some reason, the currently running ad, when complete, will not hit the rewardBasedVideoAd method.
So the solution to this should be to remove loading another ad if the ad is not ready. I would suggest loading another ad right after the rewardBasedVideoAd is completed instead.
So change this:
if rewardBasedVideo?.isReady == true {
rewardBasedVideo?.present(fromRootViewController: self)
if !adRequestInProgress && rewardBasedVideo?.isReady == false {
rewardBasedVideo?.load(GADRequest(),
withAdUnitID: "ca-app-pub-3940256099942544/1712485313")
//ca-app-pub-8020832369894951/1361675226
adRequestInProgress = true
}
to this:
if rewardBasedVideo?.isReady == true {
rewardBasedVideo?.present(fromRootViewController: self)
}