WKWebView'u kurma

Uygulamanız iOS WKWebView web içeriğini göstermek için içerikten en iyi şekilde para kazanılmasını sağlamak için yapılandırılması önerilir.

Bu kılavuzda, bir yapılandırmanın nasıl yapılandırılacağı ve WKWebView nesnesi.

Web ayarları

Varsayılan WKWebView ayarları reklamlar için optimize edilmemiştir. Tekliflerinizi otomatikleştirmek ve optimize etmek için WKWebViewConfiguration ve WKWebView API'lerini kullanarak aşağıdaki kaynaklar için web görünümünüzü yapılandırın:

  • Satır içi oynatma
  • Otomatik video oynatma
  • Bağlantı önizlemelerine izin vermeme

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)

    // Links opened using link preview don't call web view delegates. Ensure
    // delegates are always called on clicks by disabling link preview.
    webView.allowsLinkPreviews = false
    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];

  // Links opened using link preview don't call web view delegates. Ensure
  // delegates are always called on clicks by disabling link preview.
  self.webView.allowsLinkPreviews = NO;
  [self.view addSubview:self.webView];
}

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

Çerezler ve sayfa URL'leri, web görünümünden para kazanma açısından önemlidir. ile kullanıldığında beklendiği gibi ağ tabanlı URL'dir. Optimize edilmiş WKWebView performans için, . ağ tabanlı bir URL'den web içeriği 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)

    // Links opened using link preview don't call web view delegates. Ensure
    // delegates are always called on clicks by disabling link preview.
    webView.allowsLinkPreviews = false
    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];

  // Links opened using link preview don't call web view delegates. Ensure
  // delegates are always called on clicks by disabling link preview.
  self.webView.allowsLinkPreviews = NO;
  [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];
}
.

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

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

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

bu ayarların reklamlar üzerinde amaçlanan etkiye sahip olduğunu doğrulayın. Test URL'si aşağıdaki durum gözlemlenirse tam entegrasyon için başarı kriterleri:

Web görünümü ayarları

  • Birinci taraf çerezleri çalışır
  • JavaScript etkin

Video reklam

  • Video reklam satır içinde oynatılır ve tam ekranda yerleşik olarak açılmaz oynatıcı
  • Video reklam, oynat düğmesi tıklanmadan otomatik olarak oynatılır
  • Video reklam tekrar oynatılabilir

Test tamamlandıktan sonra, test URL'sini web görünümünün URL'siyle değiştirin yüklenmeyi planladığı anlamına gelir.