WKWebView'u kurma

Uygulamanız web içeriğini göstermek için WKWebView kullanıyorsa içeriğin reklamlarla en iyi şekilde para kazandıracak şekilde yapılandırılması önerilir.

Bu kılavuzda, WKWebView nesnesinin nasıl yapılandırılacağı hakkında bilgi sağlamanın yolu gösterilmektedir.

WKWebView

Medya İçeriği

Varsayılan WKWebView ayarları, video reklamlar için optimize edilmemiştir. Satır içi oynatma ve otomatik video oynatma için WKWebView yapılandırmak üzere WKWebViewConfiguration API'lerini kullanın.

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

Web görünümü içeriğini yükleme

Çerezler ve sayfa URL'leri, web görünümü para kazanma için önemlidir ve yalnızca load(_:) ağ tabanlı bir URL ile kullanıldığında beklendiği gibi çalışır. Optimize edilmiş WKWebView performans için web içeriğini ağ tabanlı bir URL'den yüklemenizi önemle tavsiye ederiz.

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://google.github.io/webview-ads/test/") 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://google.github.io/webview-ads/test/"];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  [webView loadRequest:request];
}

Web görünümünü test etme

Uygulama geliştirme sırasında şu test URL'sini yüklemenizi öneririz:

https://google.github.io/webview-ads/test/

Bu ayarların reklamlar üzerinde amaçlanan etkiyi sağladığını doğrulamak için. Aşağıdakiler gözlemlenirse test URL'si, tam entegrasyon için başarı ölçütlerine sahiptir:

Web görünümü ayarları

  • Birinci taraf çerezleri çalışır
  • JavaScript etkin olmalıdır.

Video reklam

  • Video reklam satır içinde oynatılıyor ve yerleşik tam ekran oynatıcıda açılmıyor
  • Video reklam, oynat düğmesi tıklanmadan otomatik olarak oynatılır.
  • Video reklam yeniden oynatılabilir

Test tamamlandıktan sonra test URL'sini, web görünümünün yüklemeyi amaçladığı URL ile değiştirin.