整合廣告適用的 WebView API

廣告專用的 Web View API 會向WKWebView中的代碼提供應用程式信號,協助提供內容的發布商提升營利成效,並防止廣告客戶收到垃圾內容。

運作方式

只有在回應下列任一項目觸發的廣告事件時,系統才會與 Google Mobile Ads SDK 通訊:

SDK 會將訊息處理常式新增至已註冊的WKWebView ,以監聽這些廣告事件。如要進一步瞭解相關運作方式,請查看測試頁面的原始碼

必要條件

  • Google Mobile Ads SDK 9.6.0 以上版本。
  • 使用下列鍵和字串值更新 Info.plist 檔案。這麼做會略過 Google Mobile Ads SDK 針對在網頁檢視畫面外導入廣告的 GADApplicationIdentifier 值所執行的檢查。如果您錯過這個步驟且未提供 GADApplicationIdentifier,Google Mobile Ads SDK 會在應用程式啟動時擲回 GADInvalidInitializationException

    <!-- Indicate Google Mobile Ads SDK usage is only for web view APIs for ads -->
    <key>GADIntegrationManager</key>
    <string>webview</string>
    

註冊 Web 檢視畫面

在主執行緒上呼叫 register(_:) ,與每個 WKWebView 執行個體中的 JavaScript 處理常式或 Google 發布商廣告代碼建立連線。請盡早完成,例如在檢視控制器的 viewDidLoad 方法中。

Swift

import WebKit

class ViewController: UIViewController {

  var webView: WKWebView!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Initialize a WKWebViewConfiguration object.
    let webViewConfiguration = WKWebViewConfiguration()
    // Let HTML videos with a "playsinline" attribute play inline.
    webViewConfiguration.allowsInlineMediaPlayback = true
    // Let HTML videos with an "autoplay" attribute play automatically.
    webViewConfiguration.mediaTypesRequiringUserActionForPlayback = []

    // Initialize the WKWebView with your WKWebViewConfiguration object.
    webView = WKWebView(frame: view.frame, configuration: webViewConfiguration)
    view.addSubview(webView)

    // Register the web view.
    GADMobileAds.sharedInstance().register(webView)
  }
}

Objective-C

@import WebKit;

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic, strong) WKWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // Initialize a WKWebViewConfiguration object.
  WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];
  // Let HTML videos with a "playsinline" attribute play inline.
  webViewConfiguration.allowsInlineMediaPlayback = YES;
  // Let HTML videos with an "autoplay" attribute play automatically.
  webViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;

  // Initialize the WKWebView with your WKWebViewConfiguration object.
  self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webViewConfiguration];
  [self.view addSubview:self.webView];

  // Register the web view.
  [GADMobileAds.sharedInstance registerWebView:self.webView];
}

測試整合功能

使用您自己的網址前,建議您先載入下列網址來測試整合作業:

https://webview-api-for-ads-test.glitch.me#api-for-ads-tests

在符合下列條件的情況下,測試網址會顯示成功整合的綠色狀態列:

  • WKWebView 已連結到 Google Mobile Ads SDK

後續步驟

  • 請在「 WKWebView」中收集同意聲明。廣告的 Web View API 不會將使用 IAB 資訊公開和同意聲明架構第 2.0 版IAB CCPA 規範架構在行動應用程式環境中收集到的同意聲明,套用到網頁瀏覽次數中的代碼。如果有意以「WKWebView 」擁有者身分和對應的網路內容營利,導入單一的同意聲明流程,請與您的同意聲明管理平台合作,在 WKWebView 情境中收集同意聲明。