激励广告

请选择平台Android iOS Unity Flutter

所谓激励广告,指的是用户可以选择与之互动来换取应用内奖励的一种广告。本指南介绍了如何将 AdMob 激励广告植入到 iOS 应用中。 欢迎查看客户成功案例:案例研究 1案例研究 2

前提条件

  • Google 移动广告 SDK 8.0.0 或更高版本。
  • 完成入门指南中的步骤。

务必用测试广告进行测试

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

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

ca-app-pub-3940256099942544/1712485313

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

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

实现

植入激励广告的主要步骤如下所示:

  • 加载广告
  • [可选] 验证 SSV 回调
  • 注册回调
  • 展示广告并处理奖励事件

加载广告

广告的加载是通过针对 GADRewardedAd 类使用 load(adUnitID:request) 方法完成的。

Swift

func loadRewardedAd() async {
  do {
    rewardedAd = try await RewardedAd.load(
      // Replace this ad unit ID with your own ad unit ID.
      with: "ca-app-pub-3940256099942544/1712485313", request: Request())
    rewardedAd?.fullScreenContentDelegate = self
  } catch {
    print("Rewarded ad failed to load with error: \(error.localizedDescription)")
  }
}

SwiftUI

import GoogleMobileAds

class RewardedViewModel: NSObject, ObservableObject, FullScreenContentDelegate {
  @Published var coins = 0
  private var rewardedAd: RewardedAd?

  func loadAd() async {
    do {
      rewardedAd = try await RewardedAd.load(
        with: "ca-app-pub-3940256099942544/1712485313", request: Request())
      rewardedAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load rewarded ad with error: \(error.localizedDescription)")
    }
  }

Objective-C

// Replace this ad unit ID with your own ad unit ID.
[GADRewardedAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
              request:[GADRequest request]
    completionHandler:^(GADRewardedAd *ad, NSError *error) {
      if (error) {
        NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
        return;
      }
      self.rewardedAd = ad;
      self.rewardedAd.fullScreenContentDelegate = self;
    }];

[可选] 验证服务器端验证 (SSV) 回调

对于需要服务器端验证回调中额外数据的应用,应使用激励广告的自定义数据功能。在激励广告对象上设置的任何字符串值都将传递给 SSV 回调的 custom_data 查询参数。如果未设置自定义数据值,custom_data 查询参数值不会出现在 SSV 回调中。

以下代码示例演示了如何在请求广告之前对激励广告对象设置自定义数据:

Swift

do {
  rewardedAd = try await RewardedAd.load(
    // Replace this ad unit ID with your own ad unit ID.
    with: "ca-app-pub-3940256099942544/1712485313", request: Request())
  let options = ServerSideVerificationOptions()
  options.customRewardText = "SAMPLE_CUSTOM_DATA_STRING"
  rewardedAd?.serverSideVerificationOptions = options
} catch {
  print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}

Objective-C

// Replace this ad unit ID with your own ad unit ID.
[GADRewardedAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
                        request:[GADRequest request]
              completionHandler:^(GADRewardedAd *ad, NSError *error) {
                if (error) {
                  NSLog(@"Rewarded ad failed to load with error: %@", error.localizedDescription);
                  return;
                }
                self.rewardedAd = ad;
                GADServerSideVerificationOptions *options =
                    [[GADServerSideVerificationOptions alloc] init];
                options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING";
                ad.serverSideVerificationOptions = options;
              }];

注册回调

如需接收有关展示事件的通知,您必须分配返回的广告的 GADFullScreenContentDelegate to thefullScreenContentDelegate` 属性:

Swift

rewardedAd?.fullScreenContentDelegate = self

SwiftUI

rewardedAd?.fullScreenContentDelegate = self

Objective-C

self.rewardedAd.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 ad.
  rewardedAd = 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 ad.
  rewardedAd = 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 ad.
  self.rewardedAd = nil;
}

- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
}

展示广告并处理奖励事件

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

展示广告时,您必须提供 GADUserDidEarnRewardHandler 对象,用于处理用户奖励。

以下代码演示了展示激励广告的最佳方法:

Swift

rewardedAd.present(from: self) {
  let reward = rewardedAd.adReward
  print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")

  // TODO: Reward the user.
}

SwiftUI

监听视图中的界面事件,以确定何时展示广告。

var body: some View {
  VStack(spacing: 20) {
      Button("Watch video for additional 10 coins") {
        viewModel.showAd()
        showWatchVideoButton = false
      }

从视图模型中呈现激励广告:

func showAd() {
  guard let rewardedAd = rewardedAd else {
    return print("Ad wasn't ready.")
  }

  rewardedAd.present(from: nil) {
    let reward = rewardedAd.adReward
    print("Reward amount: \(reward.amount)")
    self.addCoins(reward.amount.intValue)
  }
}

Objective-C

[self.rewardedAd presentFromRootViewController:self
                      userDidEarnRewardHandler:^{
                        GADAdReward *reward = self.rewardedAd.adReward;
                        NSString *rewardMessage = [NSString
                            stringWithFormat:@"Reward received with currency %@ , amount %lf",
                                             reward.type, [reward.amount doubleValue]];
                        NSLog(@"%@", rewardMessage);

                        // TODO: Reward the user.
                      }];

常见问题解答

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

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

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

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

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

系统是否始终会在调用 adDidDismissFullScreenContent: 委托方法之前调用 userDidEarnRewardHandler 完成处理程序?

对于 Google Ads,所有 userDidEarnRewardHandler 调用都发生在 adDidDismissFullScreenContent: 调用之前。对于通过中介投放的广告,由第三方广告联盟 SDK 的实现情况决定回调顺序。对于为单个委托方法提供奖励信息的广告联盟 SDK,中介适配器会在调用 adDidDismissFullScreenContent: 之前调用 userDidEarnRewardHandler

GitHub 上的示例

查看以您的首选语言提供的完整激励广告示例:

后续步骤

详细了解用户隐私权