Configurare WKWebView

Se la tua iOS app utilizzaWKWebView per visualizzare contenuti web, ti consigliamo di configurarla in modo che i contenuti possano essere monetizzati in modo ottimale con gli annunci.

Questa guida mostra come fornire informazioni su come configurare un oggettoWKWebView .

Contenuti multimediali

Le impostazioni predefinite di WKWebView non sono ottimizzate per gli annunci video. Utilizza le API WKWebViewConfiguration per configurare WKWebView per la riproduzione in linea e la riproduzione video automatica.

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)
  }
}

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];
}

Carica contenuti della visualizzazione web

I cookie e gli URL delle pagine sono importanti per le prestazioni delle viste web e funzionano solo come previsto quando load(_:) viene utilizzato con un URL basato su rete. Per ottimizzare WKWebView le prestazioni, ti consigliamo vivamente di caricare i contenuti web da un URL basato su rete.

Swift

import WebKit

var webview: WKWebview!

class ViewController: UIViewController {
  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)

    // Load the URL for optimized web view performance.
    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 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];

  // Load the URL for optimized web view performance.
  NSURL *url = [NSURL URLWithString:@"https://webview-api-for-ads-test.glitch.me"];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  [webView loadRequest:request];
}

Testare la vista web

Durante lo sviluppo dell'app, ti consigliamo di caricare questo URL di test:

https://webview-api-for-ads-test.glitch.me#webview-settings-tests

per verificare che queste impostazioni abbiano l'effetto previsto sugli annunci. L'URL di test presenta criteri per un'integrazione completa se si verificano quanto segue:

Impostazioni vista web

  • I cookie proprietari funzionano
  • JavaScript attivato

Annuncio video

  • L'annuncio video viene riprodotto in linea e non si apre nel player integrato a schermo intero
  • L'annuncio video viene riprodotto automaticamente senza fare clic sul pulsante di riproduzione
  • L'annuncio video può essere riprodotto di nuovo

Al termine del test, sostituisci l'URL di test con l'URL che la vista web intende caricare.