I want to play Audio when the rewarded Ad is completed and dismissed from the ad screen.
Here is my code:-
var rewardedAd: GADRewardedAd?
override func viewDidLoad() {
super.viewDidLoad()
rewardedAd?.fullScreenContentDelegate = self
createAndLoadRewardAd()
}
Receiving the reward:
if let ad = rewardedAd {
ad.present(fromRootViewController: self) {
let reward = ad.adReward
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
Player.shared.play()
}
} else {
let alert = UIAlertController(
title: "Rewarded ad isn't available yet.",
message: "Sound Cannot Be play without watching Rewarded ad. Try after few moments or check Internet Connection",
preferredStyle: .alert)
let alertAction = UIAlertAction(
title: "OK",
style: .cancel,
handler: { [weak self] action in
})
alert.addAction(alertAction)
self.present(alert, animated: true, completion: nil)
}
The problem is sound is started before closing the Ad view. I want to play the sound after closing the ad view. I also tried the following protocol but it not working, I mean response nothing
extension SoundViewController: GADFullScreenContentDelegate {
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("ad showing")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
createAndLoadRewardAd()
print("ad closed")
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print(error.localizedDescription)
}
func createAndLoadRewardAd() {
GADRewardedAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest()
) { (ad, error) in
if let error = error {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
return
}
print("Loading Succeeded")
self.rewardedAd = ad
}
}
}
Update 1
Audio is Started at this state. But I want to start the audio after the ad view is closed.
Sorry for the short answer, I will be assuming that you are using the latest AdMob SDK. You can simply do the following:
AdMob has a method called
rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward)
for any View Controller conforming toGADRewardedAdDelegate
.You can simply add this as follows:
This code will trigger after the user has earned the reward. If you want the sound to play regardless of whether or not the user earned the reward, then you can put it in the
adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd)
method. You will not be able to check if the user actually did earn the reward if you use theadDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd)
method.