設定 WKWebView

如果您的應用程式 iOS 使用 WKWebView 來顯示網路內容, 以便運用廣告將內容調整為最佳營利狀態。

本指南將說明如何提供設定 WKWebView 物件。

網頁設定

預設的「WKWebView」設定不會針對廣告進行最佳化。使用 WKWebViewConfiguration敬上 和 WKWebView API,為下列資源設定網路檢視畫面:

  • 即時播放
  • 自動播放影片
  • 不允許預覽連結

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)

    // Links opened using link preview don't call web view delegates. Ensure
    // delegates are always called on clicks by disabling link preview.
    webView.allowsLinkPreviews = false
    view.addSubview(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];

  // Links opened using link preview don't call web view delegates. Ensure
  // delegates are always called on clicks by disabling link preview.
  self.webView.allowsLinkPreviews = NO;
  [self.view addSubview:self.webView];
}

載入網頁檢視內容

Cookie 和網頁網址是網頁收視營利的重要關鍵,僅適用於它 可與 load(_:) 搭配使用 聯播網式網址為了最佳化 WKWebView 效能 我們強烈建議您從網路式網址載入網路內容。

Swift

import WebKit

var webview: WKWebview!

class ViewController: UIViewController {
  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)

    // Links opened using link preview don't call web view delegates. Ensure
    // delegates are always called on clicks by disabling link preview.
    webView.allowsLinkPreviews = false
    view.addSubview(webView)

    // Load the URL for optimized web view performance.
    guard let url = URL(string: "https://webview-api-for-ads-test.glitch.me") else { return }
    let request = URLRequest(url: url)
    webView.load(request)
  }
}

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];

  // Links opened using link preview don't call web view delegates. Ensure
  // delegates are always called on clicks by disabling link preview.
  self.webView.allowsLinkPreviews = NO;
  [self.view addSubview:self.webview];

  // Load the URL for optimized web view performance.
  NSURL *url = [NSURL URLWithString:@"https://webview-api-for-ads-test.glitch.me"];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  [webView loadRequest:request];
}

測試網頁檢視畫面

在應用程式開發期間,建議您載入以下測試網址:

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

,確認這些設定能對廣告產生預期效果。測試網址的 成功整合的成功標準如下:

網頁檢視畫面設定

  • 第一方 Cookie 的運作方式
  • JavaScript 已啟用

影片廣告

  • 影片廣告會以內嵌方式播放,不會以全螢幕開啟 球員
  • 影片廣告會自動播放,不必按下播放按鈕
  • 影片廣告可重播

測試完成後,請將測試網址替換成網頁檢視畫面的網址 事件。