激励广告

所谓激励广告,指的是用户可以选择与之互动来换取应用内奖励的一种广告。本指南向您介绍如何在 iOS 应用中植入 AdMob 激励广告。

前提条件

创建激励广告对象

激励广告由 GADRewardedAd 对象请求和展示。要使用此对象,第一步是将其实例化并设置其广告单元 ID。例如,以下示例演示了如何在 UIViewControllerviewDidLoad: 方法中创建 GADRewardedAd

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADRewardedAdDelegate {
  /// The rewarded video ad.
  var rewardedAd: GADRewardedAd? override func viewDidLoad() {
    super.viewDidLoad()
    rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController ()

@property(nonatomic, strong) GADRewardedAd *rewardedAd;

@end

@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];

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

务必用测试广告进行测试

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

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

ca-app-pub-3940256099942544/1712485313

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

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

加载广告

要加载激励广告,请调用 GADRewardedAd 对象的 loadRequest:completionHandler: 方法。

Swift

rewardedAd?.load(GADRequest()) { error in
  self.adRequestInProgress = false
  if let error = error {
    print("Loading failed: \(error)")
  } else {
    print("Loading Succeeded")
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController ()

@property(nonatomic, strong) GADRewardedAd *rewardedAd;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  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.
    }
  }];
}

此代码使用提供的 GADRequest 和完成块加载激励广告。成功完成广告请求后,将使用空 GADRequestError 参数执行完成块。如果广告加载失败,则非空错误对象会提供失败信息。

展示广告

在向用户展示激励广告之前,必须为用户提供明确的选项,让用户可以自行选择是否通过观看激励广告内容来换取奖励。激励广告必须始终是一项可由用户自行选择的体验。

要展示激励广告,请检查 GADRewardedAd 上的 isReady 属性以验证广告是否已完成加载,然后调用 presentFromRootViewController:delegate:。以下示例演示了如何在 UIViewController 的一个操作方法中执行此操作:

Swift

@IBAction func doSomething(sender: UIButton) {
  if rewardedAd?.isReady == true {
     rewardedAd?.present(fromRootViewController: self, delegate:self)
  }
}

Objective-C

@import GoogleMobileAds;

@import UIKit;

@interface ViewController () <GADRewardedAdDelegate>

@property(nonatomic, strong) GADRewardedAd *rewardedAd;

@end

@implementation ViewController
- (IBAction)doSomething:(id)sender {
  ...
  if (self.rewardedAd.isReady) {
    [self.rewardedAd presentFromRootViewController:self delegate:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

接收广告事件通知

发生激励广告事件时,会调用 presentFromRootViewController:delegate: 方法中提供的 GADRewardedAdDelegateGADRewardedAdDelegate 中的每种方法均对应激励广告生命周期内的一个事件。rewardedAd:userDidEarnReward: 方法需要实现,但该类的所有其他方法都是可选方法,因此您只需实现所需的方法即可。以下示例实现了每个方法,并将消息记录到控制台:

Swift

/// Tells the delegate that the user earned a reward.
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
  print("Reward received with currency: \(reward.type), amount \(reward.amount).")
}
/// Tells the delegate that the rewarded ad was presented.
func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad presented.")
}
/// Tells the delegate that the rewarded ad was dismissed.
func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad dismissed.")
}
/// Tells the delegate that the rewarded ad failed to present.
func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
  print("Rewarded ad failed to present.")
}

Objective-C

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

/// Tells the delegate that the rewarded ad was presented.
- (void)rewardedAdDidPresent:(GADRewardedAd *)rewardedAd {
  NSLog(@"rewardedAdDidPresent:");
}

/// Tells the delegate that the rewarded ad failed to present.
- (void)rewardedAd:(GADRewardedAd *)rewardedAd didFailToPresentWithError:(NSError *)error {
  NSLog(@"rewardedAd:didFailToPresentWithError");
}

/// Tells the delegate that the rewarded ad was dismissed.
- (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd {
  NSLog(@"rewardedAdDidDismiss:");
}

使用 GADRewardedAdDelegate 预加载下一个激励广告

GADRewardedAd 是一次性对象。这意味着,在展示激励广告后,就不能再用该对象加载另一个广告了。要请求另一个激励广告,您需要创建新的 GADRewardedAd 对象。

最佳做法是在 GADRewardedAdDelegate 上的 rewardedAdDidDismiss: 方法中加载另一个激励广告,以便在上一个激励广告关闭后,立即开始加载下一个激励广告:

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  rewardedAd = createAndLoadRewardedAd()
}

func createAndLoadRewardedAd() {
  rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
  rewardedAd?.load(GADRequest()) { error in
    if let error = error {
      print("Loading failed: \(error)")
    } else {
      print("Loading Succeeded")
    }
  }
  return rewardedAd
}

func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
  rewardedAd = createAndLoadRewardedAd()
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];
  self.rewardedAd = [self createAndLoadRewardedAd];
}

- (GADRewardedAd *)createAndLoadRewardedAd {
  GADRewardedAd *rewardedAd = [[GADRewardedAd alloc]
      initWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];
  GADRequest *request = [GADRequest request];
  [rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
    if (error) {
      // Handle ad failed to load case.
    } else {
      // Ad successfully loaded.
    }
  }];
  return rewardedAd;
}

- (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd {
  self.rewardedAd = [self createAndLoadRewardedAd];
}

加载多个激励广告

要加载多个激励广告,请针对您要加载的每个广告,按照创建激励广告对象加载广告部分中所述的步骤操作。下面的代码段展示了如何为两个不同的广告展示位置加载两个激励广告。

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  rewardedAd1 = createAndLoadRewardedAd("first-ad-unit-id")
  rewardedAd2 = createAndLoadRewardedAd("second-ad-unit-id")
}

func createAndLoadRewardedAd(adUnitId) {
  rewardedAd = GADRewardedAd(adUnitID: adUnitId)
  rewardedAd?.load(GADRequest()) { error in
    if let error = error {
      print("Loading failed: \(error)")
    } else {
      print("Loading Succeeded")
    }
  }
  return rewardedAd
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRewardedAd *gameOverRewardedAd = [self
    createAndLoadRewardedAdForAdUnit:@"ca-app-pub-3940256099942544/1712485313"];
  GADRewardedAd *extraCoinsRewardedAd = [self
    createAndLoadRewardedAdForAdUnit:@"ca-app-pub-3940256099942544/1712485313"];
}

- (GADRewardedAd *)createAndLoadRewardedAdForAdUnit:(NSString *) adUnitId {
  GADRewardedAd *rewardedAd = [[GADRewardedAd alloc] initWithAdUnitID:adUnitId];
  GADRequest *request = [GADRequest request];
  [rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
    if (error) {
      // Handle ad failed to load case.
    } else {
      // Ad successfully loaded.
    }
  }];
  return rewardedAd;
}

常见问题解答

我可以获取 GADRewardedAd 的详细奖励信息吗?
可以,如果您需要在 userDidEarnReward 回调触发之前获得奖励金额,可以查看 GADRewardedAdreward 属性,在广告加载后验证奖励金额。
初始化调用是否会超时?
10 秒后,即使中介广告联盟仍未完成初始化,Google 移动广告 SDK 还是会调用提供给 startWithCompletionHandler: 方法的 GADInitializationCompletionHandler
在获得初始化回调时,如果某些中介广告联盟还没有准备好,该怎么办?

最佳做法是在 GADInitializationCompletionHandler 中加载广告。即使中介广告联盟尚未就绪,Google 移动广告 SDK 仍会向该广告联盟请求广告。因此,如果中介广告联盟在超时后完成初始化,它仍然可以在该会话中为将来的广告请求提供服务。

您可以通过调用 GADMobileAds.initializationStatus 继续在整个应用会话中轮询所有适配器的初始化状态。

如何找出特定中介广告联盟未做好准备的原因?

GADAdapterStatus 对象的 description 属性描述了适配器未准备好为广告请求提供服务的原因。