Tích hợp WebView API for Ads (API WebView cho quảng cáo)
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.
API WebView cho quảng cáo cung cấp các tín hiệu ứng dụng cho thẻ trong WKWebView
, giúp cải thiện khả năng kiếm tiền cho những nhà xuất bản đã cung cấp nội dung và bảo vệ nhà quảng cáo khỏi nội dung không thích hợp.

Cách hoạt động
Việc giao tiếp với SDK quảng cáo trên thiết bị di động của Google chỉ xảy ra khi phản hồi các sự kiện quảng cáo do bất kỳ sự kiện nào sau đây kích hoạt:
SDK sẽ thêm trình xử lý thông báo vào WKWebView
đã đăng ký để theo dõi các sự kiện quảng cáo này. Để hiểu rõ hơn về cách hoạt động của chức năng này, hãy xem mã nguồn của trang thử nghiệm.
Điều kiện tiên quyết
- SDK Quảng cáo của Google trên thiết bị di động
phiên bản 9.6.0 trở lên.
Cập nhật tệp Info.plist
bằng khoá và giá trị chuỗi sau. Thao tác này bỏ qua một bước kiểm tra mà SDK quảng cáo trên thiết bị di động của Google thực hiện đối với giá trị GADApplicationIdentifier
áp dụng cho những nhà phát triển triển khai quảng cáo bên ngoài một khung hiển thị web. Nếu bạn bỏ qua bước này và không cung cấp GADApplicationIdentifier
, thì SDK Quảng cáo của Google trên thiết bị di động sẽ gửi một GADInvalidInitializationException
khi khởi động ứng dụng.
<!-- Indicate Google Mobile Ads SDK usage is only for web view APIs for ads -->
<key>GADIntegrationManager</key>
<string>webview</string>
Đăng ký khung hiển thị web
Gọi register(_:)
trên luồng chính để thiết lập kết nối với các trình xử lý JavaScript trong mã AdSense hoặc Thẻ nhà xuất bản của Google trong mỗi phiên bản WKWebView
. Bạn nên thực hiện việc này càng sớm càng tốt, chẳng hạn như trong phương thức viewDidLoad
của bộ điều khiển chế độ xem.
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)
// Register the web view.
MobileAds.shared.register(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];
// Register the web view.
[GADMobileAds.sharedInstance registerWebView:self.webView];
}
Kiểm tra quá trình tích hợp
Trước khi sử dụng URL của riêng mình, bạn nên tải URL sau để kiểm tra quá trình tích hợp:
https://google.github.io/webview-ads/test/#api-for-ads-tests
URL kiểm thử sẽ hiển thị các thanh trạng thái màu xanh lục cho một hoạt động tích hợp thành công nếu bạn đáp ứng các điều kiện sau:
WKWebView
được kết nối với SDK Quảng cáo của Google trên thiết bị di động
Các bước tiếp theo
- Thu thập sự đồng ý trong
WKWebView
. API Web view cho Quảng cáo không truyền sự đồng ý được thu thập trong bối cảnh ứng dụng di động bằng cách sử dụng TCF phiên bản 2.0 của IAB hoặc khuôn khổ tuân thủ CCPA của IAB cho các thẻ trong thành phần hiển thị web của bạn. Nếu bạn muốn triển khai một quy trình đồng ý duy nhất với tư cách là chủ sở hữu của cả WKWebView
và nội dung web tương ứng đang được kiếm tiền, hãy hợp tác với nền tảng quản lý sự đồng ý của bạn để thu thập sự đồng ý trong bối cảnh WKWebView
.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-09-02 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-09-02 UTC."],[[["\u003cp\u003eWeb view APIs for ads enable app signals to be used by ad tags in your WKWebView, improving monetization for publishers and protecting advertisers.\u003c/p\u003e\n"],["\u003cp\u003eCommunication with the Google Mobile Ads SDK is triggered by ad events from AdSense code, Google Publisher Tag, or IMA for HTML5.\u003c/p\u003e\n"],["\u003cp\u003eTo enable this functionality, register your WKWebView instance using \u003ccode\u003eGADMobileAds.sharedInstance().register(webView)\u003c/code\u003e after initializing it with the proper configuration.\u003c/p\u003e\n"],["\u003cp\u003eBefore deploying, test the integration using the provided test URL to ensure proper connection between your WKWebView and the Google Mobile Ads SDK.\u003c/p\u003e\n"],["\u003cp\u003eConsent gathering for IAB TCF v2.0 and IAB CCPA needs to be handled within the WKWebView context separately.\u003c/p\u003e\n"]]],["The Web View APIs for Ads enables app signals to be available within `WKWebView` tags, enhancing publisher monetization and protecting advertisers. It works by having the Google Mobile Ads SDK add message handlers to a registered `WKWebView` that will then listen for events from AdSense code, Google Publisher Tag, or IMA for HTML5. Developers must use the SDK version 9.6.0 or higher and add a `GADIntegrationManager` key to the `Info.plist` to indicate usage. The `register(_:)` method must be called on the main thread to establish connection with the JavaScript handlers.\n"],null,["The web view APIs for ads makes app signals available to the tags in your\n\n[`WKWebView`](//developer.apple.com/documentation/webkit/wkwebview), helping to improve monetization for the\npublishers that provided the content and protect advertisers from spam.\n\n\nHow it works\n\nCommunication with the Google Mobile Ads SDK only happens in response to ad\nevents triggered by any of the following:\n\n- [AdSense code](//support.google.com/adsense/answer/9274634)\n- [Google Publisher Tag](//support.google.com/admanager/answer/181073)\n- [IMA for HTML5](//support.google.com/adsense/answer/6391192)\n\nThe SDK adds message handlers to the registered `WKWebView` to listen for\nthese ad events. For a better sense of how this works, view the\n[source code](//github.com/google/webview-ads/blob/main/test/index.html) of the\ntest page.\n| **Warning:** Don't call [`removeAllScriptMessageHandlers()`](//developer.apple.com/documentation/webkit/wkusercontentcontroller/3585114-removeallscriptmessagehandlers) from your `WKWebView` instance as this has unintended side effects. If needed, call [`removeScriptMessageHandler(forName:)`](//developer.apple.com/documentation/webkit/wkusercontentcontroller/1537532-removescriptmessagehandler) to remove individual message handlers instead.\n\nPrerequisites\n\n- [Google Mobile Ads SDK](/admob/ios/quick-start#import_the_mobile_ads_sdk) version 9.6.0 or higher.\n- Update the `Info.plist` file with the following key and string value. This\n bypasses a check the Google Mobile Ads SDK does for a\n `GADApplicationIdentifier` value that applies to developers who implement ads\n outside of a web view. If you miss this step and don't provide a\n `GADApplicationIdentifier`, the Google Mobile Ads SDK throws a\n `GADInvalidInitializationException` on app start.\n\n \u003c!-- Indicate Google Mobile Ads SDK usage is only for web view APIs for ads --\u003e\n \u003ckey\u003eGADIntegrationManager\u003c/key\u003e\n \u003cstring\u003ewebview\u003c/string\u003e\n\n\u003cbr /\u003e\n\nRegister the web view\n\nCall\n\n[`register(_:)`](/admob/ios/api/reference/Classes/GADMobileAds#-registerwebview:)\n\non the main thread to establish a connection with the JavaScript handlers in the\nAdSense code or Google Publisher Tag within each `WKWebView` instance. This\nshould be done as early as possible, such as in the\n\n`viewDidLoad` method of your view controller.\n\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 // Register the web view.\n MobileAds.shared.register(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 // Register the web view.\n \\[GADMobileAds.sharedInstance registerWebView:self.webView\\];\n }\n\nTest your integration\n\nBefore using your own URL, we recommend that you load the following URL to test\nthe integration: \n\n https://google.github.io/webview-ads/test#api-for-ads-tests\n\nThe test URL shows green status bars for a successful integration if the\nfollowing conditions apply:\n\n- `WKWebView` connected to the Google Mobile Ads SDK\n\nNext steps\n\n- Gather consent in `WKWebView`. The Web view APIs for Ads doesn't propagate consent collected in the mobile app context using [IAB TCF v2.0](//iabeurope.eu/tcf-2-0/) or [IAB CCPA](//iabtechlab.com/wp-content/uploads/2019/11/Technical-Specifications-FAQ-US-Privacy-IAB-Tech-Lab.pdf) compliance frameworks to the tags in your web views. If you're interested in implementing a single consent flow as the owner of both the `WKWebView` and its corresponding web content being monetized, work with your consent management platform to gather consent in the `WKWebView` context."]]