Để thảo luận và đưa ra ý kiến phản hồi về các sản phẩm của chúng tôi, hãy tham gia kênh Discord chính thức của AdMob trong máy chủ Cộng đồng quảng cáo và đo lường của Google.
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nếu ứng dụng của bạn sử dụng WKWebView để hiển thị nội dung trên web, thì bạn nên định cấu hình để có thể kiếm tiền tối ưu từ nội dung bằng quảng cáo.
Hướng dẫn này cho bạn biết cách cung cấp thông tin về cách định cấu hình đối tượng WKWebView.
Nội dung nghe nhìn
Chế độ cài đặt WKWebView mặc định không đượ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 chế độ phát trực tiếp và chế độ tự động phát video.
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];}
Tải nội dung chế độ xem web
Cookie và URL trang là những yếu tố quan trọng để kiếm tiền từ chế độ xem web và chỉ hoạt động như mong đợi khi load(_:) được dùng với một URL dựa trên mạng. Để tối ưu hoá hiệu suất WKWebView, bạn nên tải nội dung web từ một URL dựa trên mạng.
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];}
Kiểm thử chế độ xem web
Trong quá trình phát triển ứng dụng, bạn nên tải URL thử nghiệm này:
để xác minh rằng các chế độ cài đặt này có tác động như dự kiến đối với quảng cáo. URL kiểm thử có tiêu chí thành công cho một quy trình tích hợp hoàn chỉnh nếu bạn thấy những điều sau:
Chế độ cài đặt chế độ xem 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 cùng dòng và không mở trong trình phát tích hợp toàn màn hình
Quảng cáo dạng video tự động phát mà không cần nhấp vào nút phát
Quảng cáo dạng video có thể phát lại
Sau khi kiểm thử xong, hãy thay thế URL kiểm thử bằng URL mà khung hiển thị web dự định tải.
[null,null,["Cập nhật lần gần đây nhất: 2025-09-02 UTC."],[[["\u003cp\u003eiOS apps using WKWebView for web content should configure it for optimal ad monetization.\u003c/p\u003e\n"],["\u003cp\u003eConfigure WKWebView to enable inline video playback and autoplay for better video ad performance.\u003c/p\u003e\n"],["\u003cp\u003eLoad web content using \u003ccode\u003eload(_:)\u003c/code\u003e with a network-based URL for optimal performance and monetization functionality.\u003c/p\u003e\n"],["\u003cp\u003eUse the provided test URL during development to verify ad integration and WKWebView settings.\u003c/p\u003e\n"]]],["To optimize `WKWebView` for ad monetization, configure it using `WKWebViewConfiguration`. Enable inline playback (`allowsInlineMediaPlayback = true`) and automatic video play (`mediaTypesRequiringUserActionForPlayback = []` or `WKAudiovisualMediaTypeNone`). Load content via `load(_:)` with a network-based URL to ensure cookies and URLs function correctly. Test the setup with \"https://webview-api-for-ads-test.glitch.me#webview-settings-tests\" to confirm first-party cookies, JavaScript, and video ad functionality, then replace it with the intended URL.\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\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."]]