Stay organized with collections
Save and categorize content based on your preferences.
If your app utilizes WKWebView to display web content, it's
recommended to configure it so that content can be optimally monetized with ads.
This guide shows you how to provide information about how to configure a
WKWebView object.
Media Content
Default WKWebView settings are not optimized for video ads. Use the
WKWebViewConfiguration
APIs to configure your WKWebView for inline playback and automatic video play.
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];}
Load web view content
Cookies and page URLs are important for web view monetization and only function
as expected when
load(_:) is used with a network-based URL. For optimized
WKWebView performance,
we strongly recommend loading web content from a network-based URL.
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];}
Test the web view
During app development, we recommend that you load this test URL:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-20 UTC."],[[["\u003cp\u003eiOS apps using WKWebView for web content should configure it for optimal ad monetization.\u003c/p\u003e\n"],["\u003cp\u003eConfigure WKWebView to enable inline video playback and autoplay for better video ad performance.\u003c/p\u003e\n"],["\u003cp\u003eLoad web content using \u003ccode\u003eload(_:)\u003c/code\u003e with a network-based URL for optimal performance and monetization functionality.\u003c/p\u003e\n"],["\u003cp\u003eUse the provided test URL during development to verify ad integration and WKWebView settings.\u003c/p\u003e\n"]]],["To optimize `WKWebView` for ad monetization, configure it using `WKWebViewConfiguration`. Enable inline playback (`allowsInlineMediaPlayback = true`) and automatic video play (`mediaTypesRequiringUserActionForPlayback = []` or `WKAudiovisualMediaTypeNone`). Load content via `load(_:)` with a network-based URL to ensure cookies and URLs function correctly. Test the setup with \"https://webview-api-for-ads-test.glitch.me#webview-settings-tests\" to confirm first-party cookies, JavaScript, and video ad functionality, then replace it with the intended URL.\n"],null,["If 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\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\nSwift \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\nObjective-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\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\nSwift \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\nObjective-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\nTest the web view\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\nWeb view settings\n\n- First-party cookies work\n- JavaScript enabled\n\nVideo 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."]]