Menyiapkan WKWebView

Jika aplikasi iOS Anda menggunakan WKWebView untuk menampilkan konten web, sebaiknya konfigurasikan agar konten dapat dimonetisasi secara optimal dengan iklan.

Panduan ini menunjukkan cara memberikan informasi tentang cara mengonfigurasi WKWebView .

Setelan web

Setelan WKWebView default tidak dioptimalkan untuk iklan. Gunakan WKWebViewConfiguration dan WKWebView API untuk mengonfigurasi tampilan web Anda untuk resource berikut:

  • Pemutaran sisipan
  • Pemutaran video otomatis
  • Melarang pratinjau link

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

Memuat konten tampilan web

Cookie dan URL halaman penting untuk monetisasi tampilan web dan hanya berfungsi seperti yang diharapkan jika digunakan dengan URL berbasis jaringan. Untuk performa WKWebView yang dioptimalkan, sebaiknya Anda memuat konten web dari URL berbasis jaringan.

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

Menguji tampilan web

Selama pengembangan aplikasi, sebaiknya Anda memuat URL pengujian ini:

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

untuk memverifikasi bahwa setelan ini memiliki pengaruh yang diinginkan terhadap iklan. URL pengujian memiliki kriteria keberhasilan untuk integrasi lengkap jika memenuhi kriteria berikut:

Setelan tampilan web

  • Cookie pihak pertama berfungsi
  • JavaScript diaktifkan

Iklan video

  • Iklan video diputar secara inline dan tidak terbuka dalam layar penuh bawaan pemain
  • Iklan video akan diputar secara otomatis tanpa mengklik tombol putar
  • Iklan video dapat diputar ulang

Setelah pengujian selesai, ganti URL pengujian dengan URL tampilan web yang akan dimuat.