Rewarded Ads

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

前提条件

  • Google 移动广告 SDK 7.42.2 或更高版本。

    创建激励广告对象

    激励广告由 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:@"/6499/example/rewarded-video"];
    }
    

    务必用测试广告进行测试

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

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

    /6499/example/rewarded-video

    该测试广告单元 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:@"/6499/example/rewarded-video"];
    
      DFPRequest *request = [DFPRequest request];
      [self.rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
        if (error) {
          // Handle ad failed to load case.
        } else {
          // Ad successfully loaded.
        }
      }];
    }
    

    此代码使用提供的 DFPRequest 和完成块加载激励广告。成功完成广告请求后,将使用空 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:@"/6499/example/rewarded-video"];
      DFPRequest *request = [DFPRequest 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:@"/6499/example/rewarded-video"];
      GADRewardedAd *extraCoinsRewardedAd = [self
        createAndLoadRewardedAdForAdUnit:@"/6499/example/rewarded-video"];
    }
    
    - (GADRewardedAd *)createAndLoadRewardedAdForAdUnit:(NSString *) adUnitId {
      GADRewardedAd *rewardedAd = [[GADRewardedAd alloc] initWithAdUnitID:adUnitId];
      DFPRequest *request = [DFPRequest request];
      [rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
        if (error) {
          // Handle ad failed to load case.
        } else {
          // Ad successfully loaded.
        }
      }];
      return rewardedAd;
    }