插页式激励广告是一种激励用户的广告格式,采用这种格式时,您可以通过在应用中的自然过渡点自动展示的广告向用户提供奖励。与激励广告不同,用户无需自行选择即可观看插页式激励广告。
前提条件
- 通读入门指南。
植入步骤
植入插页式激励广告的主要步骤如下所示:
- 加载广告
- [可选] 验证 SSV 回调
- 注册回调
- 展示广告并处理奖励事件
加载广告
广告的加载是通过针对 GADRewardedInterstitialAd 类使用 load(adUnitID:request) 方法完成的。
Swift
SwiftUI
import GoogleMobileAds
class RewardedInterstitialViewModel: NSObject, ObservableObject,
  FullScreenContentDelegate
{
  @Published var coins = 0
  private var rewardedInterstitialAd: RewardedInterstitialAd?
  func loadAd() async {
    do {
      rewardedInterstitialAd = try await RewardedInterstitialAd.load(
        with: "ca-app-pub-3940256099942544/6978759866", request: Request())
      rewardedInterstitialAd?.fullScreenContentDelegate = self
    } catch {
      print(
        "Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
    }
  }
Objective-C
请将 adUnitID 替换为您实际的广告单元 ID。
[可选] 验证服务器端验证 (SSV) 回调
对于在服务器端验证回调中需要额外数据的应用,应使用激励广告的自定义数据功能。在激励广告对象上设置的任何字符串值都将传递给 SSV 回调的 custom_data 查询参数。如果未设置自定义数据值,custom_data 查询参数值不会出现在 SSV 回调中。
以下代码示例演示了如何在请求广告之前对插页式激励广告对象设置自定义数据。
Swift
Objective-C
将 SAMPLE_CUSTOM_DATA_STRING 替换为您的自定义数据。
注册回调
如需接收关于展示事件的通知,您必须将 GADFullScreenContentDelegate 分配给返回的广告的 fullScreenContentDelegate 属性:
Swift
rewardedInterstitialAd?.fullScreenContentDelegate = self
SwiftUI
rewardedInterstitialAd?.fullScreenContentDelegate = self
Objective-C
self.rewardedInterstitialAd.fullScreenContentDelegate = self;
GADFullScreenContentDelegate 协议会在广告成功展示或展示失败以及用户关闭广告时处理回调。以下代码演示了如何实现此协议并将其分配给广告:
Swift
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
  // Clear the rewarded interstitial ad.
  rewardedInterstitialAd = nil
}
func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called with error: \(error.localizedDescription).")
}
SwiftUI
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}
func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called")
}
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the rewarded interstitial ad.
  rewardedInterstitialAd = nil
}
Objective-C
- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
  // Clear the rewarded interstitial ad.
  self.rewardedInterstitialAd = nil;
}
- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
}
展示广告并处理奖励事件
展示广告时,您必须提供 GADUserDidEarnRewardHandler 对象,用于处理用户奖励。
以下代码演示了展示插页式激励广告的最佳方法。
Swift
func showRewardedInterstitialAd() {
  guard let rewardedInterstitialAd = rewardedInterstitialAd else {
    return print("Ad wasn't ready.")
  }
  // The UIViewController parameter is an optional.
  rewardedInterstitialAd.present(from: nil) {
    let reward = rewardedInterstitialAd.adReward
    print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
    // TODO: Reward the user.
  }
}
SwiftUI
监听视图中的界面事件以展示广告。
var rewardedInterstitialBody: some View {
  // ...
  }
  .onChange(
    of: showAd,
    perform: { newValue in
      if newValue {
        viewModel.showAd()
      }
    }
  )
从视图模型呈现插页式激励广告:
func showAd() {
  guard let rewardedInterstitialAd = rewardedInterstitialAd else {
    return print("Ad wasn't ready.")
  }
  rewardedInterstitialAd.present(from: nil) {
    let reward = rewardedInterstitialAd.adReward
    print("Reward amount: \(reward.amount)")
    self.addCoins(reward.amount.intValue)
  }
}
Objective-C
- (void)showRewardedInterstitialAd {
  [self.rewardedInterstitialAd presentFromRootViewController:self
                                    userDidEarnRewardHandler:^{
                                      GADAdReward *reward = self.rewardedInterstitialAd.adReward;
                                      NSString *rewardMessage = [NSString
                                          stringWithFormat:@"Reward received with "
                                                           @"currency %@ , amount %ld",
                                                           reward.type, [reward.amount longValue]];
                                      NSLog(@"%@", rewardMessage);
                                      // TODO: Reward the user.
                                    }];
}
GitHub 示例
查看以您的首选语言提供的完整插页式激励广告示例:
后续步骤
详细了解用户隐私。