The WebView API for Ads allows in-app ad monetization using
WKWebView
.
If you display web content that implements ads with
AdSense code or
Google Publisher Tag
in your app through WKWebView
, you should use this API to enable ads
monetization. To learn more, see the
AdSense
and Ad Manager policies.
- Monetize by making ad requests with the Google Mobile Ads SDK
You can monetize your app by making ad requests to Ad Manager with the Google Mobile Ads SDK by implementing ad formats for mobile app.
- Monetize by using the WebView API for Ads
If your app uses
WKWebView
to display web content that serves ads from Ad Manager or AdSense, use the WebView API for Ads to registerWKWebView
objects with the Google Mobile Ads SDK. The JavaScript in the AdSense code or Google Publisher Tag builds and sends ad requests so you don't need to make any ad requests with the SDK. Keep in mind that only the mobile web and desktop web inventory formats are available using this API.If you don't own the web content in a
WKWebView
, you are still encouraged to use this API to help protect advertisers from spam and improve monetization for the web publishers that provided the content.
Note that you can do either option, or even both, in the same app.
This guide is intended to help you integrate the WebView API for Ads into your iOS app.
Before you begin
Before you start using the WebView API for Ads, make sure you do the following:
- Use Google Mobile Ads SDK with version 9.6.0 or higher in your app.
Update the
Info.plist
file with the key and string value below to bypass a check for theGADApplicationIdentifier
. If you miss this step, the Google Mobile Ads SDK may throw aGADInvalidInitializationException
on app start.<!-- Bypass GADApplicationIdentifier check for WebView API for Ads --> <key>GADIntegrationManager</key> <string>webview</string>
Set up and register WKWebView
You need to use the
registerWebView
method provided by the
Google Mobile Ads SDK,
to establish connection with the JavaScript handlers in the
AdSense code or
Google Publisher Tag
within each WKWebView
.
You should register the WKWebView
early in your app. The example below shows
how to do it in a view controller, before the first URL is loaded.
Swift
import WebKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() ... // 1) Set up the web view. let webView = WKWebView(frame: self.view.frame) self.view.addSubview(webView) // 2) Register the web view. GADMobileAds.sharedInstance().register(webView) } }
Objective-C
#import <GoogleMobileAds/GoogleMobileAds.h> #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; ... // 1) Set up the web view. WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame]; webView.navigationDelegate = self; [self.view addSubview:webView]; // 2) Register the web view. [GADMobileAds.sharedInstance registerWebView:webView]; }
Load the URL
You can now load a URL and display your web content through WKWebView
. We
recommend that you load this test URL:
https://webview-api-for-ads-test.glitch.me/
to test the integration prior to using your own URL.
Swift
import WebKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() ... // 1) Set up the web view. let webView = WKWebView(frame: self.view.frame) self.view.addSubview(webView) // 2) Register the web view. GADMobileAds.sharedInstance().register(webView) // 3) Load URL. 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 <GoogleMobileAds/GoogleMobileAds.h> #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; ... // 1) Set up the web view. WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame]; webView.navigationDelegate = self; [self.view addSubview:webView]; // 2) Register the web view. [GADMobileAds.sharedInstance registerWebView:webView]; // 3) Load URL. NSURL *nsurl = [NSURL URLWithString:@"https://webview-api-for-ads-test.glitch.me/"]; NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl]; [webView loadRequest:nsrequest]; }
The test URL shows green status bars for a successful integration if the following conditions apply:
WKWebView
connected to the Google Mobile Ads SDK- JavaScript enabled
- First-party cookies work
View the source code
of our test URL. You can then replace the test URL with your URL. You can also
use a proxy tool such as Charles to capture your
app's HTTPS traffic and inspect the ad requests for a &scar=
parameter.