集成适用于广告的 WebView API
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
适用于广告的 WebView API 可让 WKWebView 中的代码使用应用信号,从而帮助提供内容的发布商提升创收效果,并保护广告客户免受垃圾内容的侵扰。
这些应用信号(例如应用 ID 和应用版本)有助于启用报告和定位应用内浏览器广告资源用例,而这些用例原本仅适用于应用流量。

运作方式
与 Google 移动广告 SDK 的通信仅在响应由以下任一因素触发的广告事件时发生:
SDK 会向已注册的 WKWebView
添加消息处理程序,以监听这些广告事件。如需更好地了解其运作方式,请查看测试页面的源代码。
前提条件
注册 WebView
在主线程上调用 register(_:)
,以在每个 WKWebView
实例中与 AdSense 代码或 Google 发布商代码中的 JavaScript 处理程序建立连接。应尽早执行此操作,例如在视图控制器的 viewDidLoad
方法中执行。
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];
}
测试您的集成
在使用自己的网址之前,我们建议您加载以下网址来测试集成:
https://google.github.io/webview-ads/test/#api-for-ads-tests
如果符合以下条件,测试网址会显示绿色状态栏,表示集成成功:
WKWebView
已连接到 Google 移动广告 SDK
后续步骤
- 在
WKWebView
中征求用户意见。适用于广告的 WebView API 不会使用 IAB TCF v2.0 或 IAB CCPA 合规性框架将在移动应用情境中收集的用户意见传播到 WebView 中的代码。如果您是 WKWebView
及其相应创收 Web 内容的所有者,并希望实现单一的意见征求流程,请与您的意见征求管理平台合作,在 WKWebView
情境中征求用户意见。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-23。
[null,null,["最后更新时间 (UTC):2025-08-23。"],[[["\u003cp\u003eThe Web View APIs for Ads enable app signals to be available to ad tags within your WKWebView, enhancing monetization for publishers and protecting advertisers from spam.\u003c/p\u003e\n"],["\u003cp\u003eThese signals, such as app ID and version, unlock functionalities like reporting and targeted in-app browser inventory, previously exclusive to app traffic.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize the APIs, you must register your WKWebView with the Google Mobile Ads SDK (version 9.6.0 or higher) and update your Info.plist file.\u003c/p\u003e\n"],["\u003cp\u003eThe SDK establishes communication with JavaScript handlers in your ad code (AdSense, Google Publisher Tag, or IMA for HTML5) solely during ad events.\u003c/p\u003e\n"],["\u003cp\u003eEnsure to gather user consent within the WKWebView context as the APIs do not automatically propagate consent collected in the mobile app using IAB frameworks.\u003c/p\u003e\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\nThese app signals, such as app ID and app version, help activate [Reporting](//support.google.com/admanager/answer/14137220#report-on-in-app-webview-traffic)\nand [Target in-app browser inventory](//support.google.com/admanager/answer/14137220#target-in-app-webview-inventory)\nuse cases that are otherwise only available on app traffic.\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](/ad-manager/mobile-ads-sdk/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."]]