Nếu iOS ứng dụng của bạn sử dụng
WKWebView
để hiển thị nội dung web, bạn
nên định cấu hình ứng dụng đó để nội dung có thể kiếm tiền một cách tối ưu bằng quảng cáo.
Hướng dẫn này trình bày cách cung cấp thông tin về cách định cấu hình đối tượngWKWebView
.
Nội dung nghe nhìn
Các chế độ cài đặt WKWebView
mặc định chưa được tối ưu hoá cho quảng cáo dạng video. Sử dụng các API WKWebViewConfiguration
để định cấu hình WKWebView
cho việc phát tại chỗ và phát video tự động.
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];
}
Tải nội dung trong chế độ xem web
Cookie và URL trang rất quan trọng đối với việc kiếm tiền từ lượt xem web và chỉ hoạt động
như dự kiến khi load(_:)
được sử dụng với
URL dựa trên mạng. Để tối ưu hoá WKWebView
hiệu suất,
Bạn nên tải nội dung trên web từ một URL dựa trên mạng.
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];
}
Kiểm thử chế độ xem trên web
Trong quá trình phát triển ứng dụng, bạn nên tải URL thử nghiệm này:
https://webview-api-for-ads-test.glitch.me#webview-settings-tests
để xác minh rằng các chế độ cài đặt này có tác động mong muốn đối với quảng cáo. URL thử nghiệm có các tiêu chí thành công để tích hợp hoàn chỉnh nếu quan sát thấy những điều sau:
Cài đặt chế độ xem trên web
- Cookie của bên thứ nhất hoạt động
- Đã bật JavaScript
Quảng cáo dạng video
- Quảng cáo dạng video phát nội tuyến và không mở trong trình phát tích hợp toàn màn hình
- Quảng cáo video phát tự động mà không cần nhấp vào nút phát
- Quảng cáo video có thể phát lại
Sau khi quá trình kiểm thử hoàn tất, hãy thay thế URL kiểm thử bằng URL mà chế độ xem web dự định tải.