AI-generated Key Takeaways
-
The web view APIs for ads make app signals available to tags in
WKWebViewto improve monetization and protect against spam. -
Communication with the Google Mobile Ads SDK is triggered by ad events from AdSense code, Google Publisher Tag, or IMA for HTML5.
-
To use the web view APIs for ads, you need Google Mobile Ads SDK version 9.6.0 or higher and must update your
Info.plistfile. -
You need to call
register(_:)on the main thread to establish a connection with the JavaScript handlers in eachWKWebViewinstance. -
You can test your integration using a provided test URL.
The web view APIs for ads makes app signals available to the tags in your WKWebView
, helping to improve monetization for the
publishers that provided the content and protect advertisers from spam.
How it works
Communication with the Google Mobile Ads SDK only happens in response to ad events triggered by any of the following:
The SDK adds message handlers to the registered WKWebView
to listen for
these ad events. For a better sense of how this works, view the source code
of the
test page.
Prerequisites
- Google Mobile Ads SDK version 9.6.0 or higher.
-
Update the
Info.plistfile with the following key and string value. This bypasses a check the Google Mobile Ads SDK does for aGADApplicationIdentifiervalue that applies to developers who implement ads outside of a web view. If you miss this step and don't provide aGADApplicationIdentifier, the Google Mobile Ads SDK throws aGADInvalidInitializationExceptionon app start.<!-- Indicate Google Mobile Ads SDK usage is only for web view APIs for ads --> <key>GADIntegrationManager</key> <string>webview</string>
Register the web view
Call register(_:)
on the main thread to establish a connection with the JavaScript handlers in the
AdSense code or Google Publisher Tag within each WKWebView
instance. This
should be done as early as possible, such as in the viewDidLoad
method of your view controller.
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
)
// Register the web view.
MobileAds
.
shared
.
register
(
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
];
// Register the web view.
[
GADMobileAds
.
sharedInstance
registerWebView
:
self
.
webView
];
}
Test your integration
Before using your own URL, we recommend that you load the following URL to test the integration:
https://google.github.io/webview-ads/test/#api-for-ads-tests
The test URL shows green status bars for a successful integration if the following conditions apply:
-
WKWebViewconnected to the Google Mobile Ads SDK
Next steps
- Gather consent in
WKWebView. The Web view APIs for Ads doesn't propagate consent collected in the mobile app context using IAB TCF v2.0 or IAB CCPA compliance frameworks to the tags in your web views. If you're interested in implementing a single consent flow as the owner of both theWKWebViewand its corresponding web content being monetized, work with your consent management platform to gather consent in theWKWebViewcontext.

