Integrar a API WebView para anúncios

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

    <!-- 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.
    GADMobileAds.sharedInstance().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://webview-api-for-ads-test.glitch.me#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.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 the WKWebView and its corresponding web content being monetized, work with your consent management platform to gather consent in the WKWebView context.