<br />

The web view APIs for ads makes app signals available to the tags in your

[`WKWebView`](https://developer.apple.com/documentation/webkit/wkwebview), helping to improve monetization for the
publishers that provided the content and protect advertisers from spam.


![](https://developers.google.com/static/ad-manager/mobile-ads-sdk/images/webview/monetization-paths-paw.svg)

### How it works

Communication with the Google Mobile Ads SDK only happens in response to ad
events triggered by any of the following:

- [AdSense code](https://support.google.com/adsense/answer/9274634)
- [Google Publisher Tag](https://support.google.com/admanager/answer/181073)
- [IMA for HTML5](https://support.google.com/adsense/answer/6391192)

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](https://github.com/google/webview-ads/blob/main/test/index.html) of the
test page.

> [!WARNING]
> **Warning:** Don't call [`removeAllScriptMessageHandlers()`](https://developer.apple.com/documentation/webkit/wkusercontentcontroller/3585114-removeallscriptmessagehandlers) from your `WKWebView` instance as this has unintended side effects. If needed, call [`removeScriptMessageHandler(forName:)`](https://developer.apple.com/documentation/webkit/wkusercontentcontroller/1537532-removescriptmessagehandler) to remove individual message handlers instead.

### Prerequisites

- [Google Mobile Ads SDK](https://developers.google.com/admob/ios/quick-start.md.txt#import_the_mobile_ads_sdk) version 9.6.0 or later.
- Update the `Info.plist` file with the following key and string value. This
  bypasses a check the Google Mobile Ads SDK does for a
  `GADApplicationIdentifier` value that applies to developers who implement ads
  outside of a web view. If you miss this step and don't provide a
  `GADApplicationIdentifier`, the Google Mobile Ads SDK throws a
  `GADInvalidInitializationException` on app start.

  ```xml
  <!-- Indicate Google Mobile Ads SDK usage is only for web view APIs for ads -->
  <key>GADIntegrationManager</key>
  <string>webview</string>
  ```

<br />

### Register the web view

Call

[`register(_:)`](https://developers.google.com/admob/ios/api/reference/Classes/GADMobileAds.md.txt#-registerwebview:)

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

```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

```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:

- `WKWebView` connected 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.3](https://iabeurope.eu/transparency-consent-framework/) or [IAB CCPA](https://iabtechlab.com/wp-content/uploads/2019/11/Technical-Specifications-FAQ-US-Privacy-IAB-Tech-Lab.pdf) compliance frameworks to the tags in your web views. If you're interested in implementing a single consent flow as the owner of both the `WKWebView` and its corresponding web content being monetized, work with your consent management platform to gather consent in the `WKWebView` context.