הגדרת WKWebView

אם האפליקציה שלכם משתמשת ב-WKWebView כדי להציג תוכן אינטרנט, מומלץ להגדיר אותה כך שתוכלו לייצר הכנסות מהתוכן בצורה אופטימלית באמצעות מודעות.

במדריך הזה נסביר איך לספק מידע על הגדרת אובייקט WKWebView.

תוכן מדיה

הגדרות ברירת המחדל של WKWebView לא מותאמות למודעות וידאו. משתמשים בממשקי ה-API של WKWebViewConfiguration כדי להגדיר את WKWebView להפעלה בתוך דף ולהפעלה אוטומטית של סרטונים.

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

טעינת תוכן בתצוגת האינטרנט

קובצי cookie וכתובות URL של דפים חשובים למונטיזציה של צפיות באינטרנט, והם פועלים כצפוי רק כשמשתמשים ב-load(_:) עם כתובת URL מבוססת-רשת. כדי לשפר את הביצועים של WKWebView, מומלץ מאוד לטעון תוכן אינטרנט מכתובת URL מבוססת-רשת.

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

בדיקת תצוגת האינטרנט

במהלך פיתוח האפליקציה, מומלץ לטעון את כתובת ה-URL הזו לבדיקה:

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

כדי לוודא שההגדרות האלה משפיעות על המודעות כפי שרצית. כתובת ה-URL לבדיקה עומדת בקריטריונים להשלמת השילוב אם מתקיימים התנאים הבאים:

הגדרות תצוגה באינטרנט

  • איך פועלים קובצי cookie מהדומיין הנוכחי
  • JavaScript מופעל

מודעת וידאו

  • מודעת הווידאו מופעלת בתוך הדף ולא נפתחת בנגן המובנה במסך מלא
  • מודעת הווידאו מופעלת באופן אוטומטי בלי ללחוץ על לחצן ההפעלה
  • אפשר להפעיל מחדש את מודעת הווידאו

אחרי השלמת הבדיקה, מחליפים את כתובת ה-URL לבדיקה בכתובת ה-URL שצפויה להיטען בתצוגת האינטרנט.