> ## Documentation Index
> Fetch the complete documentation index at: https://docs.adrop.io/llms.txt
> Use this file to discover all available pages before exploring further.

# WebView Guide

> How to display backfill ads in WKWebView on iOS

## Overview

Display backfill ads in your WKWebView by registering it with the Adrop SDK. This allows web content within your app to show ads through the Adrop platform.

***

## Info.plist Configuration

Add the following key to your `Info.plist` to enable backfill ads in WebView:

```xml Info.plist theme={null}
<key>GADIntegrationManager</key>
<string>webview</string>
```

<Warning>
  This configuration is required for backfill ads to work properly in WebView. Without it, backfill ads may not display.
</Warning>

***

## Register WebView

<CodeGroup>
  ```swift Swift theme={null}
  import WebKit
  import AdropAds

  class WebViewController: UIViewController {

      @IBOutlet weak 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)
          // Register WebView for backfill ads support
          // Call as early as possible, before loading content
          Adrop.registerWebView(webView)

          // Load your web content
          if let url = URL(string: "https://your-website.com") {
              webView.load(URLRequest(url: url))
          }
      }
  }
  ```

  ```objc Objective-C theme={null}
  @import WebKit;
  @import AdropAds;

  @interface WebViewController ()
  @property (nonatomic, weak) IBOutlet WKWebView *webView;
  @end

  @implementation WebViewController

  - (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 WebView for backfill ads support
      // Call as early as possible, before loading content
      [Adrop registerWebView:self.webView];

      // Load your web content
      NSURL *url = [NSURL URLWithString:@"https://your-website.com"];
      [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
  }

  @end
  ```
</CodeGroup>

<Note>
  `registerWebView()` must be called on the main thread.
  Call this method as early as possible (e.g., in `viewDidLoad`), before loading any web content.
</Note>

<Warning>
  Backfill ads in WebView requires `adrop-ads-backfill` module. If the module is not installed, `registerWebView()` is silently ignored.
</Warning>

***

## Handling External URLs

If your app opens non-own-domain URLs in an external browser, you must ensure that ad resource requests (e.g., `googleads.g.doubleclick.net`) are not blocked.

Ad resources such as iframes and scripts are loaded automatically by the ad SDK — they are not user-initiated navigations. Only redirect **user-initiated main frame navigations** to an external browser.

<CodeGroup>
  ```swift Swift theme={null}
  import UIKit
  import WebKit

  let allowedHost = "my-domain.com"

  func webView(_ webView: WKWebView,
               decidePolicyFor navigationAction: WKNavigationAction,
               decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

      guard let url = navigationAction.request.url else {
          decisionHandler(.cancel)
          return
      }

      let host = url.host ?? ""

      // Only redirect user-initiated navigations to external browser
      // Ad resources (e.g., googleads.g.doubleclick.net) load via iframes/scripts,
      // not user clicks — do not block them
      if !host.contains(allowedHost),
         navigationAction.navigationType == .linkActivated {
          UIApplication.shared.open(url, options: [:], completionHandler: nil)
          decisionHandler(.cancel)
          return
      }

      decisionHandler(.allow)
  }
  ```

  ```objc Objective-C theme={null}
  NSString *allowedHost = @"my-domain.com";

  - (void)webView:(WKWebView *)webView
      decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
      decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

      NSURL *url = navigationAction.request.URL;
      if (!url) {
          decisionHandler(WKNavigationActionPolicyCancel);
          return;
      }

      NSString *host = url.host ?: @"";

      // Only redirect user-initiated navigations to external browser
      // Ad resources (e.g., googleads.g.doubleclick.net) load via iframes/scripts,
      // not user clicks — do not block them
      if (![host containsString:allowedHost] &&
          navigationAction.navigationType == WKNavigationTypeLinkActivated) {
          [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
          decisionHandler(WKNavigationActionPolicyCancel);
          return;
      }

      decisionHandler(WKNavigationActionPolicyAllow);
  }
  ```
</CodeGroup>

<Warning>
  Do not block or redirect all non-own-domain requests. Ad resources like `googleads.g.doubleclick.net` are loaded automatically during the backfill ad process and must be allowed to load within the WebView.
</Warning>

***

## Related Documentation

<CardGroup cols={2}>
  <Card title="Banner Ads" icon="rectangle-ad" href="/sdk/ios/banner">
    Learn how to integrate banner ads
  </Card>

  <Card title="Reference" icon="book" href="/sdk/ios/reference">
    API reference
  </Card>

  <Card title="Examples" icon="code" href="/sdk/ios/examples">
    Example repository
  </Card>
</CardGroup>
