如果您的应用使用 WKWebView 展示 Web 内容,建议您对其进行配置,以便通过广告实现内容的最佳创收效果。
本指南介绍如何提供有关如何配置 WKWebView 对象的信息。
媒体内容
默认的 WKWebView 设置并未针对视频广告进行优化。使用 WKWebViewConfiguration API 配置 WKWebView,以便实现内嵌式播放和自动播放视频。
Swift
importWebKitclassViewController:UIViewController{varwebView:WKWebView!overridefuncviewDidLoad(){super.viewDidLoad()// Initialize a WKWebViewConfiguration object.letwebViewConfiguration=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
@importWebKit;#import "ViewController.h"@interfaceViewController()@property(nonatomic,strong)WKWebView*webView;@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];// Initialize a WKWebViewConfiguration object.WKWebViewConfiguration*webViewConfiguration=[[WKWebViewConfigurationalloc]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=[[WKWebViewalloc]initWithFrame:self.view.frameconfiguration:webViewConfiguration];[self.viewaddSubview:self.webView];}
加载 WebView 内容
Cookie 和网页网址对于 WebView 创收至关重要,只有在 load(_:) 与基于网络的网址搭配使用时,才能按预期方式发挥作用。为了优化 WKWebView 性能,我们强烈建议从基于网络的网址加载 Web 内容。
Swift
importWebKitvarwebview:WKWebview!classViewController:UIViewController{overridefuncviewDidLoad(){super.viewDidLoad()// Initialize a WKWebViewConfiguration object.letwebViewConfiguration=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.guardleturl=URL(string:"https://google.github.io/webview-ads/test/")else{return}letrequest=URLRequest(url:url)webView.load(request)}}
Objective-C
@importWebKit;#import "ViewController.h"@interfaceViewController()@property(nonatomic,strong)WKWebView*webView;@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];// Initialize a WKWebViewConfiguration object.WKWebViewConfiguration*webViewConfiguration=[[WKWebViewConfigurationalloc]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=[[WKWebViewalloc]initWithFrame:self.view.frameconfiguration:webViewConfiguration];[self.viewaddSubview:self.webview];// Load the URL for optimized web view performance.NSURL*url=[NSURLURLWithString:@"https://google.github.io/webview-ads/test/"];NSURLRequest*request=[NSURLRequestrequestWithURL:url];[webViewloadRequest:request];}
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eTo optimize iOS app monetization with ads in WKWebView, configure it for inline video playback and automatic play.\u003c/p\u003e\n"],["\u003cp\u003eLoad web content using network-based URLs for optimal WKWebView performance and monetization.\u003c/p\u003e\n"],["\u003cp\u003eIf loading content from other sources, include the Publisher Provided Identifier (PPID) and page URL to minimize monetization impact.\u003c/p\u003e\n"],["\u003cp\u003eUse the provided test URL during development to verify settings and ad functionality before replacing it with your intended URL.\u003c/p\u003e\n"]]],["To optimize `WKWebView` for ad monetization, configure each instance with `WKWebViewConfiguration`. Enable inline playback for HTML videos using `allowsInlineMediaPlayback = true`, and allow automatic video play by setting `mediaTypesRequiringUserActionForPlayback` to an empty array (Swift) or `WKAudiovisualMediaTypeNone` (Objective-C). Load web content using `load(_:)` with a network-based URL for proper cookie and URL functionality. During development, test with `https://webview-api-for-ads-test.glitch.me#webview-settings-tests` to verify cookie, JavaScript, inline, autoplay, and replayable video ad settings.\n"],null,["# Set up WKWebView\n\nIf your app utilizes `\n`[WKWebView](//developer.apple.com/documentation/webkit/wkwebview) to display web content, it's\nrecommended to configure it so that content can be optimally monetized with ads.\n\nThis guide shows you how to provide information about how to configure a\n`WKWebView` object.\n| **Important:** To properly set up and optimize `WKWebView`, apply all of the following recommendations to each `WKWebView` instance in your app.\n\nMedia Content\n-------------\n\nDefault `WKWebView` settings are not optimized for video ads. Use the\n[`WKWebViewConfiguration`](//developer.apple.com/documentation/webkit/wkwebviewconfiguration)\nAPIs to configure your `WKWebView` for inline playback and automatic video play. \n\n### Swift\n\n import WebKit\n\n class ViewController: UIViewController {\n\n var webView: WKWebView!\n\n override func viewDidLoad() {\n super.viewDidLoad()\n\n // Initialize a WKWebViewConfiguration object.\n let webViewConfiguration = WKWebViewConfiguration()\n // Let HTML videos with a \"playsinline\" attribute play inline.\n webViewConfiguration.allowsInlineMediaPlayback = true\n // Let HTML videos with an \"autoplay\" attribute play automatically.\n webViewConfiguration.mediaTypesRequiringUserActionForPlayback = []\n\n // Initialize the WKWebView with your WKWebViewConfiguration object.\n webView = WKWebView(frame: view.frame, configuration: webViewConfiguration)\n view.addSubview(webView)\n }\n }\n\n### Objective-C\n\n @import WebKit;\n\n #import \"ViewController.h\"\n\n @interface ViewController ()\n\n @property(nonatomic, strong) WKWebView *webView;\n\n @end\n\n @implementation ViewController\n\n - (void)viewDidLoad {\n [super viewDidLoad];\n\n // Initialize a WKWebViewConfiguration object.\n WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];\n // Let HTML videos with a \"playsinline\" attribute play inline.\n webViewConfiguration.allowsInlineMediaPlayback = YES;\n // Let HTML videos with an \"autoplay\" attribute play automatically.\n webViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;\n\n // Initialize the WKWebView with your WKWebViewConfiguration object.\n self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webViewConfiguration];\n [self.view addSubview:self.webView];\n }\n\nLoad web view content\n---------------------\n\nCookies and page URLs are important for web view monetization and only function\nas expected when\n[`load(_:)`](//developer.apple.com/documentation/webkit/wkwebview/1414954-load) is used with a network-based URL. For optimized\n`WKWebView` performance,\nwe strongly recommend loading web content from a network-based URL.\n\n\n### Swift\n\n import WebKit\n\n var webview: WKWebview!\n\n class ViewController: UIViewController {\n override func viewDidLoad() {\n super.viewDidLoad()\n\n // Initialize a WKWebViewConfiguration object.\n let webViewConfiguration = WKWebViewConfiguration()\n // Let HTML videos with a \"playsinline\" attribute play inline.\n webViewConfiguration.allowsInlineMediaPlayback = true\n // Let HTML videos with an \"autoplay\" attribute play automatically.\n webViewConfiguration.mediaTypesRequiringUserActionForPlayback = []\n\n // Initialize the WKWebView with your WKWebViewConfiguration object.\n webView = WKWebView(frame: view.frame, configuration: webViewConfiguration)\n view.addSubview(webView)\n\n // Load the URL for optimized web view performance.\n guard let url = URL(string: \"https://google.github.io/webview-ads/test/\") else { return }\n let request = URLRequest(url: url)\n webView.load(request)\n }\n }\n\n### Objective-C\n\n @import WebKit;\n\n #import \"ViewController.h\"\n\n @interface ViewController ()\n\n @property(nonatomic, strong) WKWebView *webView;\n\n @end\n\n @implementation ViewController\n\n - (void)viewDidLoad {\n [super viewDidLoad];\n\n // Initialize a WKWebViewConfiguration object.\n WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];\n // Let HTML videos with a \"playsinline\" attribute play inline.\n webViewConfiguration.allowsInlineMediaPlayback = YES;\n // Let HTML videos with an \"autoplay\" attribute play automatically.\n webViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;\n\n // Initialize the WKWebView with your WKWebViewConfiguration object.\n self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webViewConfiguration];\n [self.view addSubview:self.webview];\n\n // Load the URL for optimized web view performance.\n NSURL \\*url = \\[NSURL URLWithString:@\"https://google.github.io/webview-ads/test/\"\\];\n NSURLRequest \\*request = \\[NSURLRequest requestWithURL:url\\];\n \\[webView loadRequest:request\\];\n }\n\n| **Tip:** If you need to load web view content from others means, such as HTML, you can mitigate the negative impact on monetization by transmitting the [Publisher Provided Identifier](//support.google.com/admanager/answer/2880055#setting_the_identifier) (PPID) and the relevant page URL ([GPT](/publisher-tag/guides/passback-tags#specify_page_url) \\| [AdSense](/adsense/management/reference/rest/v2/Dimension)) along with your web view request. These changes mitigate the monetization loss compared to loading web content from a network-based URL.\n\nTest the web view\n-----------------\n\nDuring app development, we recommend that you load this test URL: \n\n https://google.github.io/webview-ads/test/\n\nto verify these settings have the intended effect on ads. The test URL has\nsuccess criteria for a complete integration if the following are observed:\n\n#### Web view settings\n\n- First-party cookies work\n- JavaScript enabled\n\n#### Video ad\n\n- The video ad plays inline and does not open in the full screen built-in player\n- The video ad plays automatically without clicking the play button\n- The video ad is replayable\n\nAfter testing is complete, substitute the test URL with the URL the web view\nintends to load."]]