激励视频广告

激励视频广告是一种全屏视频广告,用户可选择使用全屏模式观看,以换取应用内奖励

本指南介绍了如何将 AdMob 激励视频广告植入到 iOS 应用中。

前提条件

请求激励视频广告

GADRewardBasedVideoAd 具有单实例设计,因此以下示例显示了向共享实例提出的广告加载请求:

Swift

GADRewardBasedVideoAd.sharedInstance().load(GADRequest(),
    withAdUnitID: "ca-app-pub-3940256099942544/1712485313")

Objective-C

[[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request]
    withAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];

我们建议您尽早(例如在应用代理的 application:didFinishLaunchingWithOptions: 方法中)调用加载请求,以便可以预加载视频。

务必用测试广告进行测试

在开发和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的帐号被暂停。

对于 iOS 激励视频广告,加载测试广告最简便的方法就是使用下面的专用测试广告单元 ID:

ca-app-pub-3940256099942544/1712485313

该测试广告单元 ID 已经过专门配置,可以为每个请求返回测试广告,您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。只需确保您会在发布应用前用自己的广告单元 ID 替换该测试广告单元 ID 即可。

如需详细了解移动广告 SDK 的测试广告如何运作,请参阅测试广告

设置事件通知

要设置事件通知,请在加载请求调用之前插入下面以粗体显示的代码行:

Swift

GADRewardBasedVideoAd.sharedInstance().delegate = self
GADRewardBasedVideoAd.sharedInstance().load(GADRequest(),
    withAdUnitID: "ca-app-pub-3940256099942544/1712485313")

Objective-C

[GADRewardBasedVideoAd sharedInstance].delegate = self;
[[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request]
                                       withAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];

GADRewardBasedVideoAdDelegate 会将激励视频广告生命周期事件通知您。您需要在加载广告之前设置代理。此代理中最重要的事件是 rewardBasedVideoAd:didRewardUserWithReward:,当用户因观看视频而应获得奖励时会调用该事件。您可以选择在此代理中实现其他方法。

以下示例展示了如何记录 GADRewardBasedVideoAdDelegate: 中可用的每个事件。

Swift

func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
    didRewardUserWith reward: GADAdReward) {
  print("Reward received with currency: \(reward.type), amount \(reward.amount).")
}

func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd:GADRewardBasedVideoAd) {
  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 rewardBasedVideoAdDidCompletePlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
  print("Reward based video ad has completed.")
}

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.")
}

Objective-C

- (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd
    didRewardUserWithReward:(GADAdReward *)reward {
  NSString *rewardMessage =
      [NSString stringWithFormat:@"Reward received with currency %@ , amount %lf",
          reward.type,
          [reward.amount doubleValue]];
  NSLog(rewardMessage);
}

- (void)rewardBasedVideoAdDidReceiveAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  NSLog(@"Reward based video ad is received.");
}

- (void)rewardBasedVideoAdDidOpen:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  NSLog(@"Opened reward based video ad.");
}

- (void)rewardBasedVideoAdDidStartPlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  NSLog(@"Reward based video ad started playing.");
}

- (void)rewardBasedVideoAdDidCompletePlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  NSLog(@"Reward based video ad has completed.");
}

- (void)rewardBasedVideoAdDidClose:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  NSLog(@"Reward based video ad is closed.");
}

- (void)rewardBasedVideoAdWillLeaveApplication:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  NSLog(@"Reward based video ad will leave application.");
}

- (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd
    didFailToLoadWithError:(NSError *)error {
  NSLog(@"Reward based video ad failed to load.");
}

展示激励视频广告

您最好在尝试展示激励视频广告前确保其已加载完毕。isReady 方法可指示激励视频广告请求是否已成功完成:

Swift

if GADRewardBasedVideoAd.sharedInstance().isReady == true {
  GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)
}

Objective-C

if ([[GADRewardBasedVideoAd sharedInstance] isReady]) {
  [[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:self];
}

重新加载激励视频广告

在展示完上一个激励视频广告后,您可以非常方便地在 rewardBasedVideoAdDidClose: 中加载新的激励视频广告:

Swift

func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
  GADRewardBasedVideoAd.sharedInstance().load(GADRequest(),
      withAdUnitID: "ca-app-pub-3940256099942544/1712485313")
}

Objective-C

- (void)rewardBasedVideoAdDidClose:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  [[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request]
           withAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];
}

其他资源

GitHub 上的激励视频广告示例:

Swift Objective-C

“Mobile Ads Garage”视频教程

Codelab

后续步骤

发送以下问题的反馈:

此网页
需要帮助?请访问我们的支持页面