這份文件已更新。
中文(香港) 的翻譯尚未完成。
英文更新時間:2021年9月16日
中文(香港) 更新時間:2019年5月31日

在 iOS 應用程式加入獎勵式影片廣告

擁有全螢幕體驗的獎勵式影片廣告,可讓用戶自行選擇是否要透過觀看影片來取得獎勵(例如虛擬貨幣、應用程式內物品和隱藏內容等)。廣告體驗長達 15-30 秒,不可中途略過且會展示附有行動呼籲按鈕的結束圖卡。用戶看完整段影片後,您會收到授予用戶建議獎勵的回呼。

以下是如何在 iOS 版 Audience Network 實作獎勵式影片廣告的詳細資訊。

請注意,iOS 9 以上版本才支援獎勵式影片廣告。

設定 SDK

Audience Network 獎勵式影片格式現已包含在公開 SDK 中。獎勵式影片很快將適用於所有遊戲應用程式。如果您在獲利管理工具中看不到獎勵式影片,而且您使用的是最新版 SDK,請立即申請

確保您在開始操作前,已經先行參閱 Audience Network 新手指南iOS 新手指南

實作方式

現在,在 View Controller 標頭檔(如果您是 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

在 View Controller 中新增函數,於顯示廣告前,初始化獎勵式影片物件並快取影片廣告創意

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 布林值旗幟,可讓您以動畫方式顯示。這個旗幟預設會設定為 YEStrue,但您可以將其覆寫。

在模擬器上執行時,預設會顯示測試廣告。若要在裝置上啟用測試廣告,請在載入廣告前加入下行程式碼: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 實作方式

您可以在使用 loadAd 方法之前或之後設定獎勵資料(USER_IDCURRENCY)。兩者都以下方的程式碼片段表示。

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