Configurare WKWebView

如果您的 iOS 应用使用了 WKWebView 来展示 Web 内容, 建议您对其进行配置,以便通过广告以最优方式从内容中获利。

本指南介绍如何提供有关如何配置 WKWebView 对象。

Web 设置

默认的WKWebView设置未针对广告进行优化。使用 WKWebViewConfigurationWKWebView 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

视频广告

  • 视频广告以内嵌方式播放,且无法在内置模式下打开 球员
  • 视频广告无需点击播放按钮即可自动播放
  • 该视频广告可重放

测试完成后,将测试网址替换为网页数据视图的网址 预期加载的内容