開始使用

必要條件

  • Xcode 13 以上版本。

本指南說明如何叫用 PAL SDK 接收隨機碼,以及監控播放事件。如要查看使用 PAL 生成隨機值的範例應用程式,請從 GitHub 下載 iOS 範例。

在專案中新增 PAL SDK

使用 Swift Package Manager 安裝 PAL SDK

程式輔助存取權程式庫 SDK 從 2.5.3 版開始支援 Swift Package Manager。請按照下列步驟匯入 Swift 封裝。

  1. 在 Xcode 中,依序前往「File」>「Add Packages…」(「檔案」>「新增套件…」),安裝 PAL SDK Swift Package。

  2. 使用隨即出現的提示詞框,搜尋「PAL SDK Swift Package GitHub repository」:

    https://github.com/googleads/swift-package-manager-google-programmatic-access-library-ios
    
  3. 選取要使用的 PAL SDK Swift Package 版本。 新專案建議使用「Up to Next Major Version」。

完成後,Xcode 會解析套件依附元件,並在背景下載。如要進一步瞭解如何新增套件依附元件,請參閱 Apple 的文章。

手動下載及安裝 PAL SDK

如不想使用 Swift Package Manager,可以下載 PAL SDK,然後手動新增至專案。

  1. 下載並解壓縮 PAL SDK for iOS
  2. 請按照 Apple 開發人員指南,將架構納入專案。

產生 Nonce

「Nonce」是 PAL 使用 PALNonceLoader 產生的單一加密字串。PAL SDK 規定每個新串流都必須附上新產生的隨機碼。不過,在同一個串流中,隨機碼可重複用於多個廣告請求。

以下所有程式碼片段都是對 ViewController.m 的修改,位於 PAL iOS 範例應用程式中。

如要要求 Nonce,請先匯入 PAL 程式庫:

@import ProgrammaticAccessLibrary;

您仍可控制串流關聯器 (或 &scor),每個新串流都應重設一次。同一串流的所有廣告請求應共用相同的 PALNonceLoader 和 Correlator 值,展示頻率上限和競爭排除條件功能才能正常運作。

接著,請建立 PALNonceLoader 的執行個體,並為兩個委派方法新增虛設常式:

@interface ViewController () <PALNonceLoaderDelegate>
// The nonce loader to use for nonce requests.
@property(nonatomic) PALNonceLoader *nonceLoader;
// The view in which a video would play. In this sample, it is mocked for
// simplification.
@property(nonatomic, weak) IBOutlet UIView *videoView;
@end
...
- (void) viewDidLoad {
  [super viewDidLoad];
  // The default value for 'directedForChildOrUnknownAge' is 'NO'.
  // Update the value after the appropriate consent has been gathered.
  // By default, PAL automatically determines whether to enable limited ads
  // based on the user's TCF (Transparency and Consent Framework) consent data
  // on the device. If you must manually override the default behavior,
  // for example, to meet your app's requirements, use the
  // `PALSettings.forceLimitedAds` property.
  PALSettings *settings = [[PALSettings alloc] init];
  settings.directedForChildOrUnknownAge = NO;

  self.nonceLoader = [[PALNonceLoader alloc] initWithSettings:settings];
  self.nonceLoader.delegate = self;
}

#pragma mark - PALNonceLoaderDelegate methods

- (void)nonceLoader:(PALNonceLoader *)nonceLoader
            withRequest:(PALNonceRequest *)request
    didLoadNonceManager:(PALNonceManager *)nonceManager {
}

- (void)nonceLoader:(PALNonceLoader *)nonceLoader
         withRequest:(PALNonceRequest *)request
    didFailWithError:(NSError *)error {
}

接著,啟動 Nonce 要求、填入屬性,並用來初始化 Nonce 管理工具:

@interface ViewController () <PALNonceLoaderDelegate>
// The nonce loader to use for nonce requests.
@property(nonatomic) PALNonceLoader *nonceLoader;
// The nonce manager result from the last successful nonce request.
@property(nonatomic) PALNonceManager *nonceManager;
// The view in which a video would play. In this sample, it is mocked for
// simplification.
@property(nonatomic, weak) IBOutlet UIView *videoView;
@end
...
- (void)viewDidLoad {
  ...
  self.nonceLoader.delegate = self;
  [self requestNonceManager];
}
...
#pragma mark - UI Callback methods

/**
 * Requests a new nonce manager with a request containing arbitrary test values
 * like a user might supply. Displays the nonce or error on success. This
 * should be called once per stream.
 *
 * The PALNonceRequest parameters set here are example parameters.
 * You should set your parameters based on your own app characteristics.
 */
- (void)requestNonceManager {
  PALNonceRequest *request = [[PALNonceRequest alloc] init];
  request.continuousPlayback = PALFlagOff;
  request.descriptionURL = [NSURL URLWithString:@"https://example.com/desc?key=val"];
  request.iconsSupported = YES;
  request.playerType = @"AwesomePlayer";
  request.playerVersion = @"4.2.1";
  request.PPID = @"123987456";
  request.sessionID = @"Sample SID";
  // Sample API framework integers. See reference docs for more details.
  request.supportedAPIFrameworks = [NSMutableSet setWithArray:@[ @2, @7, @9 ]];
  request.videoPlayerHeight = 480;
  request.videoPlayerWidth = 640;
  request.willAdAutoPlay = PALFlagOn;
  request.willAdPlayMuted = PALFlagOff;
  request.OMIDPartnerName = @"SamplePartner";
  request.OMIDPartnerVersion = @"6.2.1";

  if (self.nonceManager) {
    // Detach the old nonce manager's gesture recognizer before destroying it.
    [self.videoView removeGestureRecognizer:self.nonceManager.gestureRecognizer];
    self.nonceManager = nil;
  }
  [self.nonceLoader loadNonceManagerWithRequest:request];
}

最後,填入隨機碼載入器委派,記錄產生的隨機碼:

#pragma mark - PALNonceLoaderDelegate methods

- (void)nonceLoader:(PALNonceLoader *)nonceLoader
            withRequest:(PALNonceRequest *)request
    didLoadNonceManager:(PALNonceManager *)nonceManager {
  NSLog(@"Programmatic access nonce: %@", nonceManager.nonce);
  // Capture the created nonce manager and attach its gesture recognizer to the video view.
  self.nonceManager = nonceManager;
  [self.videoView addGestureRecognizer:self.nonceManager.gestureRecognizer];
}

- (void)nonceLoader:(PALNonceLoader *)nonceLoader
         withRequest:(PALNonceRequest *)request
    didFailWithError:(NSError *)error {
  NSLog(@"Error generating programmatic access nonce: %@", error);
}

進行直接 VAST 呼叫 (DVC) 時,請將隨機碼設為 givn 參數的值。Nonce 可安全用於網址,不需要另外進行編碼。

如果您向 Google 請求並放送廣告,就必須放送 AdChoices 圖示和疊加層。如要進一步瞭解如何剖析 VAST 回應及顯示圖示,請參閱「AdChoices 圖示和疊加層」。

最後,您需要新增方法,處理將內容播放工作階段資訊和點擊次數傳送至 SDK 的作業。請參閱下列範例,瞭解如何實作 sendPlaybackStart、sendPlaybackEnd 和 sendAdClick 方法:

...
// Reports the start of playback for the current content session.
- (void)sendPlaybackStart {
  [self.nonceManager sendPlaybackStart];
}

// Reports the end of playback for the current content session.
- (void)sendPlaybackEnd {
  [self.nonceManager sendPlaybackEnd];
}

// Reports an ad click for the current nonce manager, if not nil.
- (void)sendAdClick {
  [self.nonceManager sendAdClick];
}

在實作中,當首次啟動播放作業時,應在「影片播放器啟動」時呼叫 sendPlaybackStart,以回應使用者啟動的動作 (隨點即播) 或應用程式啟動的動作 (自動播放);播放作業結束時應呼叫 sendPlaybackEnd,而每次觀眾點按廣告時,則應呼叫 sendAdClick。

(選用) 透過第三方廣告伺服器傳送 Google Ad Manager 信號

設定第三方廣告伺服器對 Ad Manager 的請求。完成下列步驟後,隨機碼參數會從 PAL SDK 傳播,經過中繼伺服器,然後傳送至 Google Ad Manager。這有助於透過 Google Ad Manager 提高營利。

設定第三方廣告伺服器,在伺服器向 Ad Manager 發出的請求中加入隨機碼。以下是在第三方廣告伺服器中設定的廣告代碼範例:

https://pubads.serverside.net/gampad/ads?givn=%%custom_key_for_google_nonce%%&...

詳情請參閱 Google Ad Manager 伺服器端導入指南。

Ad Manager 會尋找 givn=,以找出隨機碼值。第三方廣告伺服器必須支援自己的巨集 (例如 %%custom_key_for_google_nonce%%),並將其替換為您在上一個步驟中提供的隨機值查詢參數。如要進一步瞭解如何完成這項操作,請參閱第三方廣告伺服器的說明文件。