Untuk mendiskusikan dan memberikan masukan tentang produk kami, bergabunglah ke channel Discord Ad Manager resmi di server Komunitas Iklan dan Pengukuran Google.
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Jika aplikasi Anda menggunakan WKWebView untuk menampilkan konten web, sebaiknya konfigurasikan agar konten dapat dimonetisasi secara optimal dengan iklan.
Panduan ini menunjukkan cara memberikan informasi tentang cara mengonfigurasi objek
WKWebView.
Konten Media
Setelan WKWebView default tidak dioptimalkan untuk iklan video. Gunakan
WKWebViewConfiguration
API untuk mengonfigurasi WKWebView untuk pemutaran inline dan pemutaran video otomatis.
Swift
importWebKitclassViewController:UIViewController{varwebView:WKWebView!overridefuncviewDidLoad(){super.viewDidLoad()// Initialize a WKWebViewConfiguration object.letwebViewConfiguration=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
@importWebKit;#import "ViewController.h"@interfaceViewController()@property(nonatomic,strong)WKWebView*webView;@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];// Initialize a WKWebViewConfiguration object.WKWebViewConfiguration*webViewConfiguration=[[WKWebViewConfigurationalloc]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=[[WKWebViewalloc]initWithFrame:self.view.frameconfiguration:webViewConfiguration];[self.viewaddSubview:self.webView];}
Memuat konten tampilan web
Cookie dan URL halaman penting untuk monetisasi tampilan web dan hanya berfungsi
seperti yang diharapkan jika
load(_:) digunakan dengan URL berbasis jaringan. Untuk performa WKWebViewyang dioptimalkan, sebaiknya muat konten web dari URL berbasis jaringan.
Swift
importWebKitvarwebview:WKWebview!classViewController:UIViewController{overridefuncviewDidLoad(){super.viewDidLoad()// Initialize a WKWebViewConfiguration object.letwebViewConfiguration=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.guardleturl=URL(string:"https://google.github.io/webview-ads/test/")else{return}letrequest=URLRequest(url:url)webView.load(request)}}
Objective-C
@importWebKit;#import "ViewController.h"@interfaceViewController()@property(nonatomic,strong)WKWebView*webView;@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];// Initialize a WKWebViewConfiguration object.WKWebViewConfiguration*webViewConfiguration=[[WKWebViewConfigurationalloc]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=[[WKWebViewalloc]initWithFrame:self.view.frameconfiguration:webViewConfiguration];[self.viewaddSubview:self.webview];// Load the URL for optimized web view performance.NSURL*url=[NSURLURLWithString:@"https://google.github.io/webview-ads/test/"];NSURLRequest*request=[NSURLRequestrequestWithURL:url];[webViewloadRequest:request];}
Menguji tampilan web
Selama pengembangan aplikasi, sebaiknya Anda memuat URL pengujian ini:
untuk memverifikasi bahwa setelan ini memiliki efek yang diinginkan pada iklan. URL pengujian memiliki kriteria keberhasilan untuk integrasi yang lengkap jika hal berikut diamati:
Setelan tampilan web
Cara kerja cookie pihak pertama
JavaScript diaktifkan
Iklan video
Iklan video diputar inline dan tidak terbuka di pemutar bawaan layar penuh
Iklan video diputar secara otomatis tanpa mengklik tombol putar
Iklan video dapat diputar ulang
Setelah pengujian selesai, ganti URL pengujian dengan URL yang akan dimuat tampilan web.
[null,null,["Terakhir diperbarui pada 2025-09-02 UTC."],[[["\u003cp\u003eTo optimize iOS app monetization with ads in WKWebView, configure it for inline video playback and automatic play.\u003c/p\u003e\n"],["\u003cp\u003eLoad web content using network-based URLs for optimal WKWebView performance and monetization.\u003c/p\u003e\n"],["\u003cp\u003eIf loading content from other sources, include the Publisher Provided Identifier (PPID) and page URL to minimize monetization impact.\u003c/p\u003e\n"],["\u003cp\u003eUse the provided test URL during development to verify settings and ad functionality before replacing it with your intended URL.\u003c/p\u003e\n"]]],["To optimize `WKWebView` for ad monetization, configure each instance with `WKWebViewConfiguration`. Enable inline playback for HTML videos using `allowsInlineMediaPlayback = true`, and allow automatic video play by setting `mediaTypesRequiringUserActionForPlayback` to an empty array (Swift) or `WKAudiovisualMediaTypeNone` (Objective-C). Load web content using `load(_:)` with a network-based URL for proper cookie and URL functionality. During development, test with `https://webview-api-for-ads-test.glitch.me#webview-settings-tests` to verify cookie, JavaScript, inline, autoplay, and replayable video ad settings.\n"],null,["If your app utilizes `\n`[WKWebView](//developer.apple.com/documentation/webkit/wkwebview) to display web content, it's\nrecommended to configure it so that content can be optimally monetized with ads.\n\nThis guide shows you how to provide information about how to configure a\n`WKWebView` object.\n| **Important:** To properly set up and optimize `WKWebView`, apply all of the following recommendations to each `WKWebView` instance in your app.\n\nMedia Content\n\nDefault `WKWebView` settings are not optimized for video ads. Use the\n[`WKWebViewConfiguration`](//developer.apple.com/documentation/webkit/wkwebviewconfiguration)\nAPIs to configure your `WKWebView` for inline playback and automatic video play. \n\nSwift \n\n import WebKit\n\n class ViewController: UIViewController {\n\n var webView: WKWebView!\n\n override func viewDidLoad() {\n super.viewDidLoad()\n\n // Initialize a WKWebViewConfiguration object.\n let webViewConfiguration = WKWebViewConfiguration()\n // Let HTML videos with a \"playsinline\" attribute play inline.\n webViewConfiguration.allowsInlineMediaPlayback = true\n // Let HTML videos with an \"autoplay\" attribute play automatically.\n webViewConfiguration.mediaTypesRequiringUserActionForPlayback = []\n\n // Initialize the WKWebView with your WKWebViewConfiguration object.\n webView = WKWebView(frame: view.frame, configuration: webViewConfiguration)\n view.addSubview(webView)\n }\n }\n\nObjective-C \n\n @import WebKit;\n\n #import \"ViewController.h\"\n\n @interface ViewController ()\n\n @property(nonatomic, strong) WKWebView *webView;\n\n @end\n\n @implementation ViewController\n\n - (void)viewDidLoad {\n [super viewDidLoad];\n\n // Initialize a WKWebViewConfiguration object.\n WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];\n // Let HTML videos with a \"playsinline\" attribute play inline.\n webViewConfiguration.allowsInlineMediaPlayback = YES;\n // Let HTML videos with an \"autoplay\" attribute play automatically.\n webViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;\n\n // Initialize the WKWebView with your WKWebViewConfiguration object.\n self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webViewConfiguration];\n [self.view addSubview:self.webView];\n }\n\nLoad web view content\n\nCookies and page URLs are important for web view monetization and only function\nas expected when\n[`load(_:)`](//developer.apple.com/documentation/webkit/wkwebview/1414954-load) is used with a network-based URL. For optimized\n`WKWebView` performance,\nwe strongly recommend loading web content from a network-based URL.\n\n\nSwift \n\n import WebKit\n\n var webview: WKWebview!\n\n class ViewController: UIViewController {\n override func viewDidLoad() {\n super.viewDidLoad()\n\n // Initialize a WKWebViewConfiguration object.\n let webViewConfiguration = WKWebViewConfiguration()\n // Let HTML videos with a \"playsinline\" attribute play inline.\n webViewConfiguration.allowsInlineMediaPlayback = true\n // Let HTML videos with an \"autoplay\" attribute play automatically.\n webViewConfiguration.mediaTypesRequiringUserActionForPlayback = []\n\n // Initialize the WKWebView with your WKWebViewConfiguration object.\n webView = WKWebView(frame: view.frame, configuration: webViewConfiguration)\n view.addSubview(webView)\n\n // Load the URL for optimized web view performance.\n guard let url = URL(string: \"https://google.github.io/webview-ads/test/\") else { return }\n let request = URLRequest(url: url)\n webView.load(request)\n }\n }\n\nObjective-C \n\n @import WebKit;\n\n #import \"ViewController.h\"\n\n @interface ViewController ()\n\n @property(nonatomic, strong) WKWebView *webView;\n\n @end\n\n @implementation ViewController\n\n - (void)viewDidLoad {\n [super viewDidLoad];\n\n // Initialize a WKWebViewConfiguration object.\n WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];\n // Let HTML videos with a \"playsinline\" attribute play inline.\n webViewConfiguration.allowsInlineMediaPlayback = YES;\n // Let HTML videos with an \"autoplay\" attribute play automatically.\n webViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;\n\n // Initialize the WKWebView with your WKWebViewConfiguration object.\n self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webViewConfiguration];\n [self.view addSubview:self.webview];\n\n // Load the URL for optimized web view performance.\n NSURL \\*url = \\[NSURL URLWithString:@\"https://google.github.io/webview-ads/test/\"\\];\n NSURLRequest \\*request = \\[NSURLRequest requestWithURL:url\\];\n \\[webView loadRequest:request\\];\n }\n\n| **Tip:** If you need to load web view content from others means, such as HTML, you can mitigate the negative impact on monetization by transmitting the [Publisher Provided Identifier](//support.google.com/admanager/answer/2880055#setting_the_identifier) (PPID) and the relevant page URL ([GPT](/publisher-tag/guides/passback-tags#specify_page_url) \\| [AdSense](/adsense/management/reference/rest/v2/Dimension)) along with your web view request. These changes mitigate the monetization loss compared to loading web content from a network-based URL.\n\nTest the web view\n\nDuring app development, we recommend that you load this test URL: \n\n https://google.github.io/webview-ads/test/\n\nto verify these settings have the intended effect on ads. The test URL has\nsuccess criteria for a complete integration if the following are observed:\n\nWeb view settings\n\n- First-party cookies work\n- JavaScript enabled\n\nVideo ad\n\n- The video ad plays inline and does not open in the full screen built-in player\n- The video ad plays automatically without clicking the play button\n- The video ad is replayable\n\nAfter testing is complete, substitute the test URL with the URL the web view\nintends to load."]]