My legacy Admob rewardedAd implementation was working fine. Today I updated to new implementation and it fails to work properly. Apart from the Admob Swift code being questionable (returning object without returning anything in function declaration)..In summary:
- I am able to create and load the initial RewardedAd
- When pressing the button that triggers the rewardedAd - via Notifications - it generates the following messages from Debugger ("Rewarded ad presented", immediately followed by "Rewarded ad failed to present" and the error message "Loading failed: Error Domain=com.google.admob Code=18 "Presentation Error: Will not present ad because ad object has been used." UserInfo={NSLocalizedDescription=Presentation Error: Will not present ad because ad object has been used.}
With regards to step 2 above, the lifecycle never goes through "rewardedAd" or "rewardedAdDidDismiss" and triggers the ""Rewarded ad failed to present" without ever attempting to present a new ad.
Not sure what the source error may be and would appreciate any help on sorting this out. thanks.
My implementation is as follows (strictly follows google code):
var rewardedAd: GADRewardedAd?
override func viewDidLoad() {
...
createAndLoadRewardedAd()
...
{
/// creating the rewarded ad
func createAndLoadRewardedAd() {
rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
rewardedAd?.load(GADRequest()) { error in
if let error = error {
print("Loading failed: \(error)")
} else {
print("Loading Succeeded")
}
}
}
/// Lifecycle
/// Tells the delegate that the user earned a reward.
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received with currency: \(reward.type), amount \(reward.amount).")
}
/// Tells the delegate that the rewarded ad was presented.
func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad presented.")
}
/// Tells the delegate that the rewarded ad was dismissed.
/// Load another ad upon dismissing the previous
func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
print("RewardAd did dismiss")
createAndLoadRewardedAd()
}
/// Tells the delegate that the rewarded ad failed to present.
func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
print("Rewarded ad failed to present.")
print("Loading failed: \(error)")
}
/// Used by Notification Observer to present rewardedAd
@objc func startRewardVideoAd() {
if rewardedAd?.isReady == true {
rewardedAd?.present(fromRootViewController: self, delegate:self)
} else {
print("Reward based video not ready")
}
}
In fact, you should use a new adUnitID before you want watch ad again.
`
`