WKWebView 설정

앱에서 iOS WKWebView 웹 콘텐츠를 표시하려면 광고를 통해 콘텐츠에서 최적의 수익을 창출할 수 있도록 구성하는 것이 좋습니다.

이 가이드에서는 WKWebView 객체입니다.

웹 설정

기본 WKWebView 설정은 광고에 최적화되어 있지 않습니다. 사용 WKWebViewConfigurationWKWebView API를 사용하여 다음 리소스의 웹 뷰를 구성합니다.

  • 인라인 재생
  • 자동 동영상 재생
  • 링크 미리보기 허용 안함

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

웹 보기 콘텐츠 로드

쿠키 및 페이지 URL은 웹 보기 수익 창출에 중요하며 기능만 작동합니다. 가 네트워크 기반 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)

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

웹 뷰 테스트

앱 개발 중에는 다음 테스트 URL을 로드하는 것이 좋습니다.

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

이 설정이 광고에 의도한 효과가 있는지 확인하세요. 테스트 URL에는 완전한 통합을 위한 성공 기준은 다음과 같습니다.

웹 보기 설정

  • 퍼스트 파티 쿠키의 작동
  • 자바스크립트가 사용 설정됨

동영상 광고

  • 동영상 광고가 인라인으로 재생되며 기본 제공되는 전체 화면에서 열리지 않습니다. 플레이어
  • 동영상 광고가 재생 버튼을 클릭하지 않아도 자동으로 재생됩니다.
  • 동영상 광고는 다시 재생할 수 있습니다.

테스트가 완료되면 테스트 URL을 웹 보기의 URL로 대체합니다. 있습니다.