文档已更新。
中文(简体) 译文尚未完成。
英语更新时间:2021年6月7日

在 iOS 应用中添加激励视频广告

激励视频广告是一种全屏体验,用户可选择观看视频广告以换取有价物,例如虚拟货币、应用内物品和独家内容等等。这类广告的长度为 15-30 秒,不可跳过,且广告的结束画面会显示行动号召。用户完整观看视频后,您将收到一个回调,以向用户发放建议的奖励。

下面详细介绍如何通过 iOS 版 Audience Network 部署激励视频广告。

请注意,仅 iOS 9 及以上版本支持激励视频广告。

设置 SDK

Audience Network 激励视频广告格式现已纳入公开 SDK。不久后,所有游戏应用均会支持激励视频广告。若您的变现管理工具中没有激励视频广告,且您的 SDK 为最新版,请立即申请

请确保先阅读 Audience Network 入门指南iOS 入门指南,然后再继续操作。

实施

现在,在您的视图控制器标头文件(若您是 Swift 用户,则为 Swift 文件)中导入 FBAudienceNetwork,声明其符合 FBRewardedVideoAdDelegate 协议,并为激励视频广告单元添加实例变量

import UIKit
import FBAudienceNetwork

class ViewController: UIViewController, FBRewardedVideoAdDelegate {
  private var rewardedVideoAd: FBRewardedVideoAd?
}
#import <UIKit/UIKit.h>
@import FBAudienceNetwork;

@interface ViewController : UIViewController <FBRewardedVideoAdDelegate>

@property (nonatomic, strong) FBRewardedVideoAd *rewardedVideoAd;

@end

在视图控制器中添加一项功能,用于初始化激励视频广告对象,并在展示前提前缓存视频创意

override func viewDidLoad() {
  super.viewDidLoad()
  
  // Instantiate an rewarded video object.
  // NOTE: the placement ID will eventually identify this as your app. You can ignore it while you are testing
  // and replace it later when you have signed up.
  // While you are using this temporary code you will only get test ads and if you release
  // your code like this to the App Store your users will not receive ads (you will get a 'No Fill' error).
  let rewardedVideoAd = FBRewardedVideoAd(placementID: "YOUR_PLACEMENT_ID")
  rewardedVideoAd.delegate = self
  
  // For auto play video ads, it's recommended to load the ad at least 30 seconds before it is shown
  rewardedVideoAd.load()
  
  self.rewardedVideoAd = rewardedVideoAd
}
- (void) viewDidLoad 
{
  [super viewDidLoad];
  // Instantiate a rewarded video ad object. 
  // NOTE: the placement ID will eventually identify this as your app. You can ignore it while 
  // you are testing and replace it later when you have signed up.
  // While you are using this temporary code you will only get test ads and if you release
  // your code like this to the App Store your users will not receive ads (you will get a 'No Fill' error).
  self.rewardedVideoAd = [[FBRewardedVideoAd alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID"];
  self.rewardedVideoAd.delegate = self;

  // For auto play video ads, it's recommended to load the ad at least 30 seconds before it is shown
  [self.rewardedVideoAd loadAd];
}

YOUR_PLACEMENT_ID 中显示的编号为暂时编号,仅供测试使用。

若您在实时代码中使用此暂时编号,您的用户将不会收到广告(他们将会收到 “未填充” 错误提示)。您必须在测试之后返回此处,将暂时编号换成实时版位编号。

若想了解如何生成实时版位编号,请参阅 Audience Network 设置

添加加载广告的代码后,即可添加处理各种事件的功能。

例如

  • rewardedVideoAdDidLoad 能够确保您的应用获知广告缓存完毕且可供展示的时间
  • rewardedVideoAdVideoComplete 可在用户完成视频观看后通知您向用户发放奖励的时间
func rewardedVideoAdDidLoad(_ rewardedVideoAd: FBRewardedVideoAd) {
  print("Video ad is loaded and ready to be displayed")
}

func rewardedVideoAd(_ rewardedVideoAd: FBRewardedVideoAd, didFailWithError error: Error) {
  print("Rewarded video ad failed to load")
}

func rewardedVideoAdDidClick(_ rewardedVideoAd: FBRewardedVideoAd) {
  print("Video ad clicked")
}

func rewardedVideoAdDidClose(_ rewardedVideoAd: FBRewardedVideoAd) {
  print("Rewarded Video ad closed - this can be triggered by closing the application, or closing the video end card")
}

func rewardedVideoAdVideoComplete(_ rewardedVideoAd: FBRewardedVideoAd) {
  print("Rewarded Video ad video completed - this is called after a full video view, before the ad end card is shown. You can use this event to initialize your reward")
}
- (void)rewardedVideoAdDidLoad:(FBRewardedVideoAd *)rewardedVideoAd
{
  NSLog(@"Video ad is loaded and ready to be displayed");
}
    
- (void)rewardedVideoAd:(FBRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error
{
  NSLog(@"Rewarded video ad failed to load");
}

- (void)rewardedVideoAdDidClick:(FBRewardedVideoAd *)rewardedVideoAd
{
  NSLog(@"Video ad clicked");
}
    
- (void)rewardedVideoAdDidClose:(FBRewardedVideoAd *)rewardedVideoAd
{
  NSLog(@"Rewarded Video ad closed - this can be triggered by closing the application, or closing the video end card");
}

- (void)rewardedVideoAdVideoComplete:(FBRewardedVideoAd *)rewardedVideoAd;
{
  NSLog(@"Rewarded Video ad video completed - this is called after a full video view, before the ad end card is shown. You can use this event to initialize your reward");
}

最后,准备好显示激励视频广告后,即可在您自己的奖励功能中调用以下代码。

private func showRewardedVideoAd() {
  guard let rewardedVideoAd = rewardedVideoAd, rewardedVideoAd.isAdValid else {
    return
  }
  rewardedVideoAd.show(fromRootViewController: self)
}
- (void)showRewardedVideoAd
{
  if (self.rewardedVideoAd && self.rewardedVideoAd.isAdValid) {
    [self.rewardedVideoAd showAdFromRootViewController:self];
  }
}

展示激励视频广告的方法包括一个 animated 布尔值标记,您可通过该标记为广告演示设置动画。默认设置为 YES / true,但您可将其撤销。

在模拟器上运行时,将默认显示测试广告。如要在设备上启用测试广告,请在加载广告前添加以下代码行:AdSettings.addTestDevice(HASHED ID)。首次请求在设备上加载广告时,需要使用输出到 Logcat 中的散列编号。

或者,您也可以添加以下额外功能,用以处理激励视频广告在何处关闭或何时捕获激励视频广告展示的情况:

func rewardedVideoAdWillClose(_ rewardedVideoAd: FBRewardedVideoAd) {
  print("The user clicked on the close button, the ad is just about to close")
}

func rewardedVideoAdWillLogImpression(_ rewardedVideoAd: FBRewardedVideoAd) {
  print("Rewarded Video impression is being captured")
}
- (void)rewardedVideoAdWillClose:(FBRewardedVideoAd *)rewardedVideoAd
{
  NSLog(@"The user clicked on the close button, the ad is just about to close");
}

- (void)rewardedVideoAdWillLogImpression:(FBRewardedVideoAd *)rewardedVideoAd
{
  NSLog(@"Rewarded Video impression is being captured");
}

服务器端奖励验证

概览

如果您在服务器端管理自己的用户奖励,Facebook 可提供解决方案,帮助您使用验证技术安全地执行此项操作。我们的服务器将与指定的 https 端点通信,以验证每一次广告展示,并验证是否应发放奖励。

  1. Audience Network SDK 要求奖励式视频广告使用以下参数:
    • Audience Network 版位编号
    • 唯一用户编号 — 用于识别唯一用户的属性。例如,数字标识符
    • 奖励价值 — 您想发放给用户的奖励的价值。例如,指定端点的 100 个金币,以及应用密钥
  2. 收到这些信息后,服务器将验证请求并作出如下响应:
    • 200 响应:请求有效,应发放奖励
    • 非 200 响应:请求无效,不应发放奖励。
  3. 用户观看完视频后,系统将显示结束画面,并触发下列事件之一。
    • onRewardServerSuccess — 仅在第 3 步收到 200 响应时触发。
    • onRewardServerFailed — 在第 3 步收到非 200 响应时触发。

Facebook 服务器将向您的发行商端点发送的网址示例。

https://www.your_end_point.com/?token=APP_SECRET&puid=USER_ID&pc=REWARD_ID&ptid=UNIQUE_TRANSACTION_ID

请向 Facebook 代表提供您的发行商端点,以便启用此功能。

SDK 部署

有可能设置奖励数据(USER_IDCURRENCY),在 loadAd 方法之前或之后。两者均在下方代码片段中展示。

let rewardedVideoAd = FBRewardedVideoAd(placementID: "YOUR_PLACEMENT_ID")
rewardedVideoAd.delegate = self

// Set the rewarded ad data before or after `load` method is called
rewardedVideoAd.setRewardDataWithUserID("USER_ID", withCurrency: "CURRENCY")

// For auto play video ads, it's recommended to load the ad at least 30 seconds before it is shown
rewardedVideoAd.load()

self.rewardedVideoAd = rewardedVideoAd
self.rewardedVideoAd = [[FBRewardedVideoAd alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID"];
self.rewardedVideoAd.delegate = self;

// Set the rewarded ad data before or after `loadAd` method is called
[self.rewardedVideoAd setRewardDataWithUserID:@"USER_ID" withCurrency:@"CURRENCY"];

[self.rewardedVideoAd loadAd];

除了 FBRewardedVideoAdDelegate 中提到的上述功能之外,还应使用以下事件来处理您应用中的奖励发放。以下内容可与上述事件共同使用。

func rewardedVideoAdServerRewardDidFail(_ rewardedVideoAd: FBRewardedVideoAd) {
  print("Rewarded video ad not validated, or no response from server")
}

func rewardedVideoAdServerRewardDidSucceed(_ rewardedVideoAd: FBRewardedVideoAd) {
  print("Rewarded video ad validated by server")
}
- (void)rewardedVideoAdServerRewardDidSucceed:(FBRewardedVideoAd *)rewardedVideoAd
{
  NSLog(@"Rewarded video ad validated by server");
}

- (void)rewardedVideoAdServerRewardDidFail:(FBRewardedVideoAd *)rewardedVideoAd
{
  NSLog(@"Rewarded video ad not validated, or no response from server");
}

请注意,服务器验证回调只会发生在用户关闭结束画面之后。在收到其中任意一个回调之前,您不应释放激励视频对象。

Next Steps