adDidDismissFullScreenContent - 如何确定广告是插页式广告还是奖励式广告?

问题描述 投票:0回答:2

我正在使用 Google AdMob v8 在同一视图控制器中实现插页式广告和奖励广告。

我需要能够确定哪个广告被忽略,以便我可以正确加载另一个广告。

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) 
    
    loadRewarded()
    loadInterstitial()
}

我上面的进度是加载奖励广告和插页式广告,无论哪个广告被驳回。

我如何确定哪个广告(插页式广告或奖励广告)被驳回?

swift admob
2个回答
9
投票
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) 
    
    print("Ad dismiss full screen ad")
    if type(of: ad) == GADInterstitialAd.self {
      print("InterstitialAd")
    }else if  type(of: ad) == GADRewardedAd.self {
      print("RewardedAd")
    }
}

0
投票

如果您有可用的广告对象,那么您还可以使用

isEqual
检查哪个添加调用了委托函数。

假设您将广告定义为类中的属性,如下所示:

class ViewController: UIViewController, GADFullScreenContentDelegate {

   private var interstitialAd: GADInterstitialAd?
   private var rewardedAd: GADRewardedAd?

   ...

然后在代码中的某个位置,您将(预)加载这些广告并为这些属性赋予一些值,因此您将执行以下操作:

func loadNextAds() {

    Task {
            do {
                interstitialAd = try await GADInterstitialAd.load(
                    withAdUnitID: <YOUR_INTERSTITIAL_ID>, request: GADRequest())
                interstitialAd?.fullScreenContentDelegate = self
                print("Next interstitial ad successfully loaded")
            } catch {
                print("Failed to load interstitial ad with error: \(error.localizedDescription)")
            }
            
            do {
                rewardedAd = try await GADRewardedAd.load(withAdUnitID: <YOUR_REWARDED_ID>, request: GADRequest())
                rewardedAd?.fullScreenContentDelegate = self
                print("Next rewarded Interstitial ad successfully loaded")
            } catch {
                print("Rewarded ad failed to load with error: \(error.localizedDescription)")
            }
        }
    }

现在,在您处理好广告呈现之后,在委托函数中,您需要做的就是比较广告是否是这些对象之一,如下所示:

    /// Tells the delegate that the ad dismissed full screen content.
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        if ad.isEqual(interstitialAd) {
            print("Interstitial ad did dismiss full screen content.")
            // Do whatever is needed in case of dismissing an interstitial ad
            
        } else if ad.isEqual(rewardedAd) {
            print("Rewarded Interstitial ad did dismiss full screen content.")
            // Do whatever is needed in case of dismissing a rewarded ad
        }
    }

但是,需要注意的是,使用当前函数呈现 AdMob 激励广告会使用闭包,并且并不总是清楚哪个将首先被命中:此闭包或

adDidDismissFullScreenContent
委托函数。看起来这取决于谁投放了广告,Google 还是他们的合作伙伴之一。
因此,我编写了一些代码,在继续执行激励广告被取消后需要运行的代码之前检查两者是否都被点击(否则,代码可能会在您希望之前在广告后面的后台开始运行)真的发生了!)

© www.soinside.com 2019 - 2024. All rights reserved.