Thiết lập chế độ xem web

Chọn nền tảng: Android (beta) Mới Android iOS

Nếu ứng dụng của bạn sử dụng WKWebView để hiển thị nội dung web, thì bạn nên định cấu hình để có thể kiếm tiền tối ưu từ nội dung bằng quảng cáo.

Hướng dẫn này trình bày cách cung cấp thông tin về cách định cấu hình đối tượng WKWebView.

Nội dung nghe nhìn

Chế độ cài đặt WKWebView mặc định không được tối ưu hoá cho quảng cáo dạng video. Sử dụng WKWebViewConfiguration API để định cấu hình WKWebView cho tính năng phát trực tiếp và tự động phát video.

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)
  }
}

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

Tải nội dung chế độ xem web

Cookie và URL trang là những yếu tố quan trọng để kiếm tiền từ chế độ xem web và chỉ hoạt động như mong đợi khi load(_:) được sử dụng với một URL dựa trên mạng. Để tối ưu hoá WKWebView hiệu suất, bạn nên tải nội dung web từ một URL dựa trên mạng.

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)
    view.addSubview(webView)

    // Load the URL for optimized web view performance.
    guard let url = URL(string: "https://google.github.io/webview-ads/test/") 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];
  [self.view addSubview:self.webview];

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

Kiểm thử chế độ xem web

Trong quá trình phát triển ứng dụng, bạn nên tải URL kiểm thử này:

https://google.github.io/webview-ads/test/

để xác minh rằng các chế độ cài đặt này có tác động như dự kiến đối với quảng cáo. URL kiểm thử có tiêu chí thành công để tích hợp hoàn chỉnh nếu bạn quan sát thấy những điều sau:

Chế độ cài đặt chế độ xem web

  • Cookie của bên thứ nhất hoạt động
  • JavaScript đã bật

Quảng cáo dạng video

  • Quảng cáo dạng video phát trực tiếp và không mở trong trình phát tích hợp toàn màn hình
  • Quảng cáo dạng video tự động phát mà không cần nhấp vào nút phát
  • Quảng cáo dạng video có thể phát lại

Sau khi kiểm thử xong, hãy thay thế URL kiểm thử bằng URL mà chế độ xem web dự định tải.