迁移至新的激励广告 API

在 iOS SDK 7.41.0 中,移动广告 SDK 开始提供新的激励广告 API。从 7.57.0 开始,旧版 API 已弃用。本指南旨在帮助发布商更新到新的激励广告 API。

加载广告

旧版 API 使用单例加载激励广告,而新的 API 则允许您创建多个 GADRewardedAd 对象并调用其加载方法。

此外,利用新版 API,您可以一次加载多个广告,这是使用旧版 GADRewardBasedVideoAd 单例无法实现的。如需详细了解如何加载多个广告,请参阅激励广告指南

旧版 API

Swift

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

func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd:GADRewardBasedVideoAd) {
  // Ad successfully loaded.
}

func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
                didFailToLoadWithError error: Error) {
  // Handle ad failed to load case.
}

Objective-C

[GADRewardBasedVideoAd sharedInstance].delegate = self;
[[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request]
                                       withAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];
...
- (void)rewardBasedVideoAdDidReceiveAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  NSLog(@"Reward based video ad is received.");
}
- (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd
    didFailToLoadWithError:(NSError *)error {
  NSLog(@"Reward based video ad failed to load.");
}

新版 API

Swift

var rewardedAd: GADRewardedAd?
override func viewDidLoad() {
  super.viewDidLoad()
  rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
  rewardedAd?.load(GADRequest()) { error in
    if let error = error {
      // Handle ad failed to load case.
    } else {
      // Ad successfully loaded.
    }
  }
}

Objective-C

...
  self.rewardedAd = [[GADRewardedAd alloc]
      initWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];

  GADRequest *request = [GADRequest request];
  [self.rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
    if (error) {
      // Handle ad failed to load case.
    } else {
      // Ad successfully loaded.
    }
  }];
...

展示广告

使用新版 API 展示广告与使用旧版 API 非常相似。主要区别在于新版 API 不再使用单例。

旧版 API

Swift

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

Objective-C

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

新版 API

Swift

if rewardedAd?.isReady == true {
   rewardedAd?.present(fromRootViewController: self, delegate:self)
}

Objective-C

if (self.rewardedAd.isReady) {
  [self.rewardedAd presentFromRootViewController:self delegate:self];
} else {
  NSLog(@"Ad wasn't ready");
}

旧版 API 的展示事件与加载的事件在同一个代理中。使用新版 API,您可以通过 presentFromRootViewController:delegate: 方法为展示事件提供一个代理。

处理广告事件

GADRewardBasedVideoAdDelegate 协议已被弃用,取而代之的是 GADRewardedAdDelegate 协议。

展示回调

新的激励广告 API 推出了展示失败的回调。此外,新版 API 还移除了 rewardBasedVideoAdWillLeaveApplication: 回调,因为 iOS 已有 applicationWillResignActive: 方法,可在您的应用即将进入闲置状态时通知您。

旧版 API

Swift

func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
  print("Opened reward based video ad.")
}

Objective-C

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

新版 API

Swift

func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad presented.")
}

func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
  print("Rewarded ad failed to present.")
}

Objective-C

- (void)rewardedAdDidPresent:(GADRewardedAd *)rewardedAd {
  NSLog(@"rewardedAdDidPresent:");
}

- (void)rewardedAd:(GADRewardedAd *)rewardedAd didFailToPresentWithError:(NSError *)error {
  NSLog(@"rewardedAd:didFailToPresentWithError");
}

关闭事件

激励广告关闭回调将 1:1 映射到新的激励广告 API。

旧版 API

Swift

func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
  print("Reward based video ad is closed.")
}

Objective-C

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

新版 API

Swift

func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad dismissed.")
}

Objective-C

- (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd {
  NSLog(@"rewardedAdDidDismiss:");
}

激励事件

激励回调将 1:1 映射到新的激励广告 API。

旧版 API

Swift

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

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);
}

新版 API

Swift

func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
  print("Reward received with currency: \(reward.type), amount \(reward.amount).")
}

Objective-C

- (void)rewardedAd:(GADRewardedAd *)rewardedAd userDidEarnReward:(GADAdReward *)reward {
  // TODO: Reward the user.
  NSLog(@"rewardedAd:userDidEarnReward:");
}

借助新版 API,您还可以在系统使用 GADRewardedAdreward 属性授予激励对象后,随时获取对该对象的引用。

中介适配器

如果您使用中介功能,请确保您使用的适配器版本不低于新的激励广告 API 所需的最低版本。最低适配器版本列表如下:

广告联盟 最低适配器版本
AdColony 3.3.7.0
AppLovin 6.3.0.0
Chartboost 7.5.0.0
Facebook Audience Network 5.2.0.1
InMobi 7.2.7.0
ironSource 6.8.0.1
maio 1.4.6.0
MoPub 5.6.0.1
myTarget 5.0.4.0
Nend 5.1.1.0
Tapjoy 12.2.1.0
Unity 3.0.0.3
Vungle 6.3.2.1