如果您的应用使用 WKWebView
展示 Web 内容,建议您对其进行配置,以便通过广告以最佳方式创收。
本指南介绍了如何提供有关如何配置 WKWebView
对象的信息。
媒体内容
默认的 WKWebView
设置未针对视频广告进行优化。使用 WKWebViewConfiguration
API 将 WKWebView
配置为内嵌播放和自动播放视频。
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];
}
加载 WebView 内容
Cookie 和网页网址对网站浏览创收至关重要,只有将 load(_:)
与基于广告联盟的网址搭配使用时,它们才能正常运行。为了优化 WKWebView
性能,我们强烈建议从基于网络的网址加载 Web 内容。
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://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];
[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 已启用
视频广告
- 视频广告在内嵌中播放,而不是在内置全屏播放器中打开
- 视频广告会在用户不点击播放按钮的情况下自动播放
- 视频广告可重玩
测试完成后,将测试网址替换为网页视图打算加载的网址。